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


import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceInvoicedOperationLogOperationTypeEnum;
import cn.superdesk.uniorder.common.excel.model.ExcelRow;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
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.customer.invoiced.InvoiceInvoiced;
import cn.superdesk.uniorder.dao.entity.invoice.customer.invoiced.InvoiceInvoicedOperationLog;
import cn.superdesk.uniorder.dao.entity.invoice.customer.invoiced.InvoiceInvoicedOperationLogExample;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.invoiced.InvoiceInvoicedOperationLogMapper;
import cn.superdesk.uniorder.dto.param.invoice.customer.invoiced.InvoiceInvoicedOperationLogQueryCriteria;
import cn.superdesk.uniorder.service.invoice.customer.invoiced.InvoiceInvoicedOperationLogService;
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-07-28 15:28:43
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-07-28 15:28:43
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class InvoiceInvoicedOperationLogServiceImpl implements InvoiceInvoicedOperationLogService {

    @Autowired
    private InvoiceInvoicedOperationLogMapper invoiceInvoicedOperationLogMapper;


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


    @Override
    public int insertSelective(InvoiceInvoicedOperationLog entity) {
        return invoiceInvoicedOperationLogMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(InvoiceInvoicedOperationLog entity) {
        return invoiceInvoicedOperationLogMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public InvoiceInvoicedOperationLog findById(Long id) {
        return invoiceInvoicedOperationLogMapper.selectByPrimaryKey(id);
    }

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

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

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


    public void addInvoiceInvoicedOperationLog(InvoiceInvoicedOperationLog entity) {
        invoiceInvoicedOperationLogMapper.insertSelective(entity);
    }

    public void updateInvoiceInvoicedOperationLog(InvoiceInvoicedOperationLog entity) {
        InvoiceInvoicedOperationLog originEntity = invoiceInvoicedOperationLogMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        invoiceInvoicedOperationLogMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteInvoiceInvoicedOperationLog(Long id) {
        invoiceInvoicedOperationLogMapper.deleteByPrimaryKey(id);
    }

    public InvoiceInvoicedOperationLog findInvoiceInvoicedOperationLogById(Long id) {
        InvoiceInvoicedOperationLog entity = invoiceInvoicedOperationLogMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

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

        // 样例 保留
//		InvoiceInvoicedOperationLog invoiceInvoicedOperationLogConditions = BeanUtils.mapToBean(conditions,InvoiceInvoicedOperationLog.class);

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

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

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

    }

    public R<List<InvoiceInvoicedOperationLog>> pageQueryGenericParam(InvoiceInvoicedOperationLogQueryCriteria queryCriteria) {

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

        if (null != queryCriteria && null != queryCriteria.getInvoiceInvoicedId()) {
            criteria.andInvoiceInvoicedIdEqualTo(queryCriteria.getInvoiceInvoicedId());
        }

        if (null != queryCriteria && null != queryCriteria.getInvoiceNumber()) {
            criteria.andInvoiceNumberEqualTo(queryCriteria.getInvoiceNumber());
        }

        example.setOrderByClause("create_time DESC");

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

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

    /****************************************** 操作日志 start ******************************************/

//	InvoiceInvoicedOperationLogCommonForm
    public <T extends ExcelRow> void commonOperationLog(InvoiceInvoiced entity, InvoiceInvoiced forUpdate, T form, LoginSysUser loginSysUser, InvoiceInvoicedOperationLogOperationTypeEnum operationTypeEnum) {

        InvoiceInvoicedOperationLog invoiceInvoicedOperationLog = new InvoiceInvoicedOperationLog();

        invoiceInvoicedOperationLog.setBatchId(form.getBatchId());
        invoiceInvoicedOperationLog.setInvoiceInvoicedId(form.getId());
        invoiceInvoicedOperationLog.setInvoiceNumber(form.getInvoiceNumber());

        invoiceInvoicedOperationLog.setCreateTime(DateUtil.date());
        invoiceInvoicedOperationLog.setCreateUserId(loginSysUser.getSysUserId());
        invoiceInvoicedOperationLog.setCreateUserName(loginSysUser.getSysUserName());

        invoiceInvoicedOperationLog.setOperationType(operationTypeEnum.getOperationTypeId());

        invoiceInvoicedOperationLog.setOperationBefore(AnJsonUtils.toJson(entity));
        invoiceInvoicedOperationLog.setOperationParams(AnJsonUtils.toJson(form));
        invoiceInvoicedOperationLog.setOperationAfter(AnJsonUtils.toJson(forUpdate));

        addInvoiceInvoicedOperationLog(invoiceInvoicedOperationLog);
    }

}
