package cn.superdesk.uniorder.service.invoice.common.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.common.enums.invoice.common.InvoiceTypeMoudelXModeEnum;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTypeModeConfigOffLine;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTypeModeConfigOffLineExample;
import cn.superdesk.uniorder.dao.mapper.invoice.common.InvoiceTypeModeConfigOffLineMapper;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigOffLineParam;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigOffLineQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeOpCriteria;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeModeConfigOffLineService;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2022-02-10 17:46:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-02-10 17:46:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class InvoiceTypeModeConfigOffLineServiceImpl implements InvoiceTypeModeConfigOffLineService {

    @Autowired
    private InvoiceTypeService invoiceTypeService;

    @Autowired
    private InvoiceTypeModeConfigOffLineMapper invoiceTypeModeConfigOffLineMapper;


    /****************************************** extends BaseService 部分 start ******************************************/
    @Override
    public void addObj(InvoiceTypeModeConfigOffLine entity) {
        invoiceTypeModeConfigOffLineMapper.insert(entity);
    }

    @Override
    public void updateObj(InvoiceTypeModeConfigOffLine entity) {
        invoiceTypeModeConfigOffLineMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public InvoiceTypeModeConfigOffLine findById(Long id) {
        return invoiceTypeModeConfigOffLineMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<InvoiceTypeModeConfigOffLine> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<InvoiceTypeModeConfigOffLine> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addInvoiceTypeModeConfigOffLine(InvoiceTypeModeConfigOffLine entity, LoginSysUser loginSysUser) {
        invoiceTypeModeConfigOffLineMapper.insertSelective(entity);
    }

    public int updateInvoiceTypeModeConfigOffLine(InvoiceTypeModeConfigOffLine entity, LoginSysUser loginSysUser) {

        int ret = 0;

        InvoiceTypeModeConfigOffLine originEntity = invoiceTypeModeConfigOffLineMapper.selectByPrimaryKey(entity.getId());

        if (ObjectUtil.isNull(originEntity)) {
            return ret;
        }

        AnBeanUtils.copy(entity, originEntity);

        originEntity.setUpdateTime(DateUtil.date());
        originEntity.setUpdateUserId(loginSysUser.getSysUserId());

        ret = invoiceTypeModeConfigOffLineMapper.updateByPrimaryKey(originEntity);

        if (ret > 0) {

            switch (CommonEnabledEnum.get(entity.getRuleEnable())) {
                case 启用:
                    invoiceTypeService.opInvoiceSettingSetEnable(InvoiceTypeOpCriteria
                            .builder()
                            .invoiceTypeId(originEntity.getInvoiceTypeId())
                            .selectByModule(originEntity.getInvoiceTypeModule())
                            .selectByMode(InvoiceTypeMoudelXModeEnum.线下开票.getKey())
                            .build(), loginSysUser);
                    break;
                case 禁用:
                    invoiceTypeService.opInvoiceSettingSetDisable(InvoiceTypeOpCriteria
                            .builder()
                            .invoiceTypeId(originEntity.getInvoiceTypeId())
                            .selectByModule(originEntity.getInvoiceTypeModule())
                            .selectByMode(InvoiceTypeMoudelXModeEnum.线下开票.getKey())
                            .build(), loginSysUser);
                    break;
            }


        }

        return ret;

    }

    public int deleteInvoiceTypeModeConfigOffLine(Long id, LoginSysUser loginSysUser) {
        return invoiceTypeModeConfigOffLineMapper.deleteByPrimaryKey(id);
    }

    public InvoiceTypeModeConfigOffLine findInvoiceTypeModeConfigOffLineById(Long id) {
        InvoiceTypeModeConfigOffLine entity = invoiceTypeModeConfigOffLineMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public R<List<InvoiceTypeModeConfigOffLine>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        InvoiceTypeModeConfigOffLineExample          example  = new InvoiceTypeModeConfigOffLineExample();
        InvoiceTypeModeConfigOffLineExample.Criteria criteria = example.createCriteria();

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 InvoiceTypeModeConfigOffLineExample.Criteria
        // InvoiceTypeModeConfigOffLine invoiceTypeModeConfigOffLineConditions = BeanUtils.mapToBean(conditions,InvoiceTypeModeConfigOffLine.class);

        PageHelper.startPage(pageNun, pageSize);
        List<InvoiceTypeModeConfigOffLine>     resultList = invoiceTypeModeConfigOffLineMapper.selectByExample(example);
        PageInfo<InvoiceTypeModeConfigOffLine> page       = new PageInfo<InvoiceTypeModeConfigOffLine>(resultList);
        PageHelper.clearPage();

        return new R<List<InvoiceTypeModeConfigOffLine>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<InvoiceTypeModeConfigOffLine>> pageQueryGenericParam(InvoiceTypeModeConfigOffLineQueryCriteria queryCriteria) {

        InvoiceTypeModeConfigOffLineExample          example  = new InvoiceTypeModeConfigOffLineExample();
        InvoiceTypeModeConfigOffLineExample.Criteria criteria = example.createCriteria();

        // TODO 添加条件... 后期添加 queryCriteria 转 InvoiceTypeModeConfigOffLineExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<InvoiceTypeModeConfigOffLine>     resultList = invoiceTypeModeConfigOffLineMapper.selectByExample(example);
        PageInfo<InvoiceTypeModeConfigOffLine> page       = new PageInfo<InvoiceTypeModeConfigOffLine>(resultList);
        PageHelper.clearPage();

        return new R<List<InvoiceTypeModeConfigOffLine>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    /****************************************** 操作部分 start ******************************************/

    public InvoiceTypeModeConfigOffLineParam getByInvoiceTypeOpCriteria(InvoiceTypeOpCriteria opCriteria, LoginSysUser loginSysUser) {

        InvoiceTypeModeConfigOffLineParam resuleVo = null;

        InvoiceTypeModeConfigOffLineExample          example  = new InvoiceTypeModeConfigOffLineExample();
        InvoiceTypeModeConfigOffLineExample.Criteria criteria = example.createCriteria();

        criteria
                .andInvoiceTypeIdEqualTo(opCriteria.getInvoiceTypeId())
                .andInvoiceTypeModuleEqualTo(opCriteria.getSelectByModule());

        List<InvoiceTypeModeConfigOffLine> resultList = invoiceTypeModeConfigOffLineMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(resultList)) {
            resuleVo = AnBeanUtils.copy(resultList.get(0), InvoiceTypeModeConfigOffLineParam.class);
        } else {
            InvoiceTypeModeConfigOffLine entity = new InvoiceTypeModeConfigOffLine();
            entity.setInvoiceTypeId(opCriteria.getInvoiceTypeId());
            entity.setInvoiceTypeModule(opCriteria.getSelectByModule());

            /****************************************** 不能为空的处理 start ******************************************/

            entity.setRuleEnable(CommonEnabledEnum.启用.getKey());
            entity.setRuleGoodsLines(0);

            /****************************************** 不能为空的处理   end ******************************************/

            int ret = invoiceTypeModeConfigOffLineMapper.insertSelective(entity);

            if (ret > 0) {
                resuleVo = AnBeanUtils.copy(entity, InvoiceTypeModeConfigOffLineParam.class);
            }
        }

        return resuleVo;
    }

    /****************************************** 操作部分   end ******************************************/

}
