package cn.superdesk.uniorder.service.organisation.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.organisation.SysSupplierExtInvoice;
import cn.superdesk.uniorder.dao.entity.organisation.SysSupplierExtInvoiceExample;
import cn.superdesk.uniorder.dao.mapper.organisation.SysSupplierExtInvoiceMapper;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierExtInvoiceQueryCriteria;
import cn.superdesk.uniorder.service.organisation.SysSupplierExtInvoiceService;
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: 2021-11-29 16:34:49
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-11-29 16:34:49
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class SysSupplierExtInvoiceServiceImpl implements SysSupplierExtInvoiceService {

    @Autowired
    private SysSupplierExtInvoiceMapper sysSupplierExtInvoiceMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(SysSupplierExtInvoice entity) {
        return sysSupplierExtInvoiceMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysSupplierExtInvoice entity) {
        return sysSupplierExtInvoiceMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysSupplierExtInvoice findById(Long id) {
        return sysSupplierExtInvoiceMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addSysSupplierExtInvoice(SysSupplierExtInvoice entity) {
        sysSupplierExtInvoiceMapper.insertSelective(entity);
    }

    public void updateSysSupplierExtInvoice(SysSupplierExtInvoice entity) {
        SysSupplierExtInvoice originEntity = sysSupplierExtInvoiceMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        sysSupplierExtInvoiceMapper.updateByPrimaryKey(originEntity);
    }

    /**
     * 关联发票信息 设置默认
     *
     * @param entity
     */
    public void updateSysSupplierExtInvoiceSetDefault(SysSupplierExtInvoice entity) {


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

        criteria
                .andSysSupplierIdEqualTo(entity.getSysSupplierId())
                .andOutSaleIdEqualTo(entity.getOutSaleId());

        SysSupplierExtInvoice forUpdate = new SysSupplierExtInvoice();
        forUpdate.setIsDefault(0);

        sysSupplierExtInvoiceMapper.updateByExampleSelective(forUpdate, example);

        entity.setIsDefault(1);

        sysSupplierExtInvoiceMapper.updateByPrimaryKeySelective(entity);

    }

    public void deleteSysSupplierExtInvoice(Long id) {
        sysSupplierExtInvoiceMapper.deleteByPrimaryKey(id);
    }

    public SysSupplierExtInvoice findSysSupplierExtInvoiceById(Long id) {
        SysSupplierExtInvoice entity = sysSupplierExtInvoiceMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

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

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 SysSupplierExtInvoiceExample.Criteria
        // SysSupplierExtInvoice sysSupplierExtInvoiceConditions = AnBeanUtils.mapToBean(conditions,SysSupplierExtInvoice.class);

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

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

    }

    public R<List<SysSupplierExtInvoice>> pageQueryGenericParam(SysSupplierExtInvoiceQueryCriteria queryCriteria) {

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

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

        if (ObjectUtil.isNotNull(queryCriteria.getOutSaleId())) {
            criteria.andOutSaleIdEqualTo(queryCriteria.getOutSaleId());
        }

        if (ObjectUtil.isNotNull(queryCriteria.getSysSupplierId())) {
            criteria.andSysSupplierIdEqualTo(queryCriteria.getSysSupplierId());
        }

        example.setOrderByClause("is_default DESC , update_time DESC");

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

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

    }

}