package cn.superdesk.uniorder.service.invoice.customer.fundrestream.impl;

import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatement;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatementOff;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatementOffExample;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapper;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapperEx;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.fundrestream.InvoiceBankStatementMapper;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.fundrestream.InvoiceBankStatementMapperEx;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.fundrestream.InvoiceBankStatementOffMapper;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.fundrestream.InvoiceBankStatementOffMapperEx;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedInvBankOffVo;
import cn.superdesk.uniorder.service.invoice.customer.fundrestream.InvoiceBankStatementOffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/7/23 下午1:52
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/7/23 下午1:52
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class InvoiceBankStatementOffServiceImpl implements InvoiceBankStatementOffService {

    @Autowired
    private InvoiceApplyMapper              invoiceApplyMapper;
    @Autowired
    private InvoiceApplyMapperEx            invoiceApplyMapperEx;
    @Autowired
    private InvoiceBankStatementMapper      invoiceBankStatementMapper;
    @Autowired
    private InvoiceBankStatementMapperEx    invoiceBankStatementMapperEx;
    @Autowired
    private InvoiceBankStatementOffMapper   invoiceBankStatementOffMapper;
    @Autowired
    private InvoiceBankStatementOffMapperEx invoiceBankStatementOffMapperEx;

    @Autowired
    private UnifiedOrderMapper   unifiedOrderMapper;
    @Autowired
    private UnifiedOrderMapperEx unifiedOrderMapperEx;


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


    @Override
    public int insertSelective(InvoiceBankStatementOff entity) {
        return invoiceBankStatementOffMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(InvoiceBankStatementOff entity) {
        return invoiceBankStatementOffMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
        invoiceBankStatementOffMapper.deleteByPrimaryKey(id);
    }

    @Override
    public InvoiceBankStatementOff findById(Long id) {
        return invoiceBankStatementOffMapper.selectByPrimaryKey(id);
    }

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

    @Override
    public List<InvoiceBankStatementOff> findAll(Map<String, Object> queryMap) {
        return invoiceBankStatementOffMapperEx.findAll(queryMap);
    }

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


    public void addInvoiceBankStatementOff(InvoiceBankStatementOff entity) {
        invoiceBankStatementOffMapper.insertSelective(entity);
    }

    public void updateInvoiceBankStatementOff(InvoiceBankStatementOff entity) {
        InvoiceBankStatementOff originEntity = invoiceBankStatementOffMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        invoiceBankStatementOffMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteInvoiceBankStatementOff(Long id) {
        invoiceBankStatementOffMapper.deleteByPrimaryKey(id);
    }

    public InvoiceBankStatementOff findInvoiceBankStatementOffById(Long id) {
        InvoiceBankStatementOff entity = invoiceBankStatementOffMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		InvoiceBankStatementOff invoiceBankStatementOffConditions = BeanUtils.mapToBean(conditions,InvoiceBankStatementOff.class);

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

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

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

    }

    /****************************************** 原来就有 start ******************************************/

    @Transactional
    @Override
    public InvoiceBankStatementOff addObjTran(InvoiceBankStatementOff unifiedInvBankOffEntity,
                                              List<InvoiceBankStatement> invoiceBankEntityTodoList,
                                              List<InvoiceApply> invoiceApplyEntityTodoList,
                                              List<UnifiedOrder> unifiedOrderEntityTodoList
    ) {
        invoiceBankStatementOffMapper.insert(unifiedInvBankOffEntity);

        for (InvoiceBankStatement item : invoiceBankEntityTodoList) {
            invoiceBankStatementMapper.updateByPrimaryKeySelective(item);
        }

        for (InvoiceApply item : invoiceApplyEntityTodoList) {
            invoiceApplyMapper.updateByPrimaryKeySelective(item);
        }

        for (UnifiedOrder item : unifiedOrderEntityTodoList) {
            unifiedOrderMapper.updateByPrimaryKeySelective(item);
        }

        return unifiedInvBankOffEntity;
    }

    @Override
    public List<Long> getOrderIdsByInvoiceApplyId(Long id) {
        return invoiceBankStatementOffMapperEx.getOrderIdsByInv(id);
    }

    @Override
    public R<List<UnifiedInvBankOffVo>> getInvoiceBankOffDataByPage(Map<String, Object> params, int pageNo, int pageSize) {

        PageHelper.startPage(pageNo, pageSize);

        List<UnifiedInvBankOffVo>     ls   = invoiceBankStatementOffMapperEx.findAllVo(params);
        PageInfo<UnifiedInvBankOffVo> page = new PageInfo<UnifiedInvBankOffVo>(ls);

        PageHelper.clearPage();

        return new R<List<UnifiedInvBankOffVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);
    }
}
