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.InvoiceTypeModeConfigSzhtxxElectronicInvoice;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTypeModeConfigSzhtxxElectronicInvoiceExample;
import cn.superdesk.uniorder.dao.mapper.invoice.common.InvoiceTypeModeConfigSzhtxxElectronicInvoiceMapper;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigSzhtxxElectronicInvoiceParam;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeModeConfigSzhtxxElectronicInvoiceQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.common.InvoiceTypeOpCriteria;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeModeConfigSzhtxxElectronicInvoiceService;
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 InvoiceTypeModeConfigSzhtxxElectronicInvoiceServiceImpl implements InvoiceTypeModeConfigSzhtxxElectronicInvoiceService {

    @Autowired
    private InvoiceTypeService invoiceTypeService;

    @Autowired
    private InvoiceTypeModeConfigSzhtxxElectronicInvoiceMapper invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper;


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

    @Override
    public void updateObj(InvoiceTypeModeConfigSzhtxxElectronicInvoice entity) {
        invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public InvoiceTypeModeConfigSzhtxxElectronicInvoice findById(Long id) {
        return invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.selectByPrimaryKey(id);
    }

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

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

    /****************************************** extends BaseService 部分   end
     * @return******************************************/


    public int addInvoiceTypeModeConfigSzhtxxElectronicInvoice(InvoiceTypeModeConfigSzhtxxElectronicInvoice entity, LoginSysUser loginSysUser) {
        return invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.insertSelective(entity);
    }

    public int updateInvoiceTypeModeConfigSzhtxxElectronicInvoice(InvoiceTypeModeConfigSzhtxxElectronicInvoice entity, LoginSysUser loginSysUser) {

        int ret = 0;

        InvoiceTypeModeConfigSzhtxxElectronicInvoice originEntity = invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.selectByPrimaryKey(entity.getId());

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

        AnBeanUtils.copy(entity, originEntity);

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

        ret = invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.updateByPrimaryKeySelective(originEntity);

        if (ret > 0) {

            if (ObjectUtil.isNull(originEntity.getRuleEnable())) {
                originEntity.setRuleEnable(CommonEnabledEnum.启用.getKey());
            }

            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 deleteInvoiceTypeModeConfigSzhtxxElectronicInvoice(Long id, LoginSysUser loginSysUser) {
        return invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.deleteByPrimaryKey(id);
    }

    public InvoiceTypeModeConfigSzhtxxElectronicInvoice findInvoiceTypeModeConfigSzhtxxElectronicInvoiceById(Long id) {
        InvoiceTypeModeConfigSzhtxxElectronicInvoice entity = invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

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

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

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

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

    }

    public R<List<InvoiceTypeModeConfigSzhtxxElectronicInvoice>> pageQueryGenericParam(InvoiceTypeModeConfigSzhtxxElectronicInvoiceQueryCriteria queryCriteria) {

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

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

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

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

    }

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

    public InvoiceTypeModeConfigSzhtxxElectronicInvoiceParam getByInvoiceTypeOpCriteria(InvoiceTypeOpCriteria opCriteria, LoginSysUser loginSysUser) {

        InvoiceTypeModeConfigSzhtxxElectronicInvoiceParam resuleVo = null;

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

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

        List<InvoiceTypeModeConfigSzhtxxElectronicInvoice> resultList = invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.selectByExample(example);

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

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

            entity.setRuleEnable(CommonEnabledEnum.禁用.getKey());
            entity.setRuleGoodsLines(999);

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

            int ret = invoiceTypeModeConfigSzhtxxElectronicInvoiceMapper.insertSelective(entity);

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

        return resuleVo;
    }

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

}
