package com.iotechn.unimall.admin.api.message;


import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.model.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.MessageDO;
import com.iotechn.unimall.data.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import java.util.Date;
import java.util.List;

/**
 * Generate Code By Unimall
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return messageMapper.deleteById(id) > 0;
    }

    @Override
    public Page<MessageDO> list(String customer, Integer page, Integer limit, Long adminId,Long companyId) throws ServiceException {
        Wrapper<MessageDO> wrapper = new EntityWrapper<MessageDO>();
        if (customer != null) {
            wrapper.like("customer", customer);
        }
        wrapper.orderBy("gmt_create",false);
        wrapper.orderBy("gmt_update",false);
        wrapper.eq("company_id",companyId);
        List<MessageDO> list = messageMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = messageMapper.selectCount(wrapper);
        return new Page<MessageDO>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageDO create(String customer, String operation, String result,  Long adminId,Long companyId) throws ServiceException {
         Date now = new Date();
         MessageDO insertDO = new MessageDO();
         insertDO.setCustomer(customer);
         insertDO.setOperation(operation);
         insertDO.setResult(result);
//         insertDO.setGmtCreate(gmtCreate);
//         insertDO.setGmtUpdate(gmtUpdate);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         insertDO.setCompanyId(companyId);
         if (messageMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String customer, String operation, String result, Long adminId,Long companyId) throws ServiceException {
         Date now = new Date();
         MessageDO updateDO = new MessageDO();
         updateDO.setId(id);
         updateDO.setCustomer(customer);
         updateDO.setOperation(operation);
         updateDO.setResult(result);
//         updateDO.setGmtCreate(gmtCreate);
//         updateDO.setGmtUpdate(gmtUpdate);
         updateDO.setGmtUpdate(now);
         updateDO.setCompanyId(companyId);
         if (messageMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
