package cn.superdesk.uniorder.dubbo.provider.organisation;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.convert.Dto2EntityConvertUtils;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExtInvoice;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseExtInvoiceParam;
import cn.superdesk.uniorder.dto.param.invoice.txt.TaxationAisinoTxtRow;
import cn.superdesk.uniorder.dto.param.organisation.SysCliqueXInvoiceCriteria;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseExtInvoiceQueryCriteria;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseExtInvoiceXCliqueParam;
import cn.superdesk.uniorder.dto.vo.organisation.SysEnterpriseExtInvoiceExtPathVo;
import cn.superdesk.uniorder.dubbo.facade.organisation.DubboSysEnterpriseExtInvoiceService;
import cn.superdesk.uniorder.service.organisation.SysEnterpriseExtInvoiceService;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 5/13/20 8:51 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 5/13/20 8:51 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboSysEnterpriseExtInvoiceServiceImpl implements DubboSysEnterpriseExtInvoiceService {

    @Autowired
    private SysEnterpriseExtInvoiceService sysEnterpriseExtInvoiceService;


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

    @Override
    public void addObj(SysEnterpriseExtInvoiceParam param) {

        SysEnterpriseExtInvoice sysEnterpriseExtInvoice = AnBeanUtils.copy(param, SysEnterpriseExtInvoice.class);
        sysEnterpriseExtInvoiceService.insertSelective(sysEnterpriseExtInvoice);

    }

    @Override
    public void updateObj(SysEnterpriseExtInvoiceParam param) {

        SysEnterpriseExtInvoice sysEnterpriseExtInvoice = AnBeanUtils.copy(param, SysEnterpriseExtInvoice.class);
        sysEnterpriseExtInvoiceService.updateByPrimaryKeySelective(sysEnterpriseExtInvoice);
    }

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

    @Override
    public SysEnterpriseExtInvoice findById(Long id) {
        return sysEnterpriseExtInvoiceService.findById(id);
    }

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

    @Override
    public List<SysEnterpriseExtInvoice> findAll(Map<String, Object> queryMap) {

        return sysEnterpriseExtInvoiceService.findAll(queryMap);
    }

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


    public void addSysEnterpriseExtInvoiceXCliqueParam(SysEnterpriseExtInvoiceXCliqueParam param, LoginSysUser loginSysUser) {

        sysEnterpriseExtInvoiceService.addSysEnterpriseExtInvoiceXCliqueParam(param, loginSysUser);
    }


    public void updateSysEnterpriseExtInvoiceXCliqueParam(SysEnterpriseExtInvoiceXCliqueParam param, LoginSysUser loginSysUser) {

        sysEnterpriseExtInvoiceService.updateSysEnterpriseExtInvoiceXCliqueParam(param, loginSysUser);
    }

    @Override
    public int addSysEnterpriseExtInvoice(SysEnterpriseExtInvoiceParam param, LoginSysUser loginSysUser) {

        SysEnterpriseExtInvoice sysEnterpriseExtInvoice = AnBeanUtils.copy(param, SysEnterpriseExtInvoice.class);
//		sysEnterpriseExtInvoice.setId(param.getSysVirtId());

        return sysEnterpriseExtInvoiceService.addSysEnterpriseExtInvoice(sysEnterpriseExtInvoice, loginSysUser);
    }

    @Override
    public int updateSysEnterpriseExtInvoice(SysEnterpriseExtInvoiceParam param, LoginSysUser loginSysUser) {

        SysEnterpriseExtInvoice sysEnterpriseExtInvoice = AnBeanUtils.copy(param, SysEnterpriseExtInvoice.class);
//		sysEnterpriseExtInvoice.setId(param.getSysVirtId());

        return sysEnterpriseExtInvoiceService.updateSysEnterpriseExtInvoice(sysEnterpriseExtInvoice, loginSysUser);
    }

    @Override
    public int opSetDefault(SysEnterpriseExtInvoice entity, LoginSysUser loginSysUser) {
        return sysEnterpriseExtInvoiceService.opSetDefault(entity, loginSysUser);
    }

    @Override
    public int opSetNormal(SysEnterpriseExtInvoice entity, LoginSysUser loginSysUser) {
        return sysEnterpriseExtInvoiceService.opSetNormal(entity, loginSysUser);
    }


    @Override
    public void deleteSysEnterpriseExtInvoice(Long id, LoginSysUser loginSysUser) {
        sysEnterpriseExtInvoiceService.deleteSysEnterpriseExtInvoice(id, loginSysUser);
    }

    @Override
    public SysEnterpriseExtInvoice findBySysVirtId(Long id) {
        return sysEnterpriseExtInvoiceService.findBySysVirtId(id);
    }

    @Override
    public SysEnterpriseExtInvoice getByCompanyName(String companyName) {
        return sysEnterpriseExtInvoiceService.getByCompanyName(companyName);
    }

    @Override
    public List<SysEnterpriseExtInvoice> findByOutVirtIds(List<Long> outVirtIds) {
        return sysEnterpriseExtInvoiceService.findByOutVirtIds(outVirtIds);
    }

    @Override
    public Map<Long, SysEnterpriseExtInvoice> findByOutVirtIdsConvertKV(List<Long> outVirtIds) {
        return sysEnterpriseExtInvoiceService.findByOutVirtIdsConvertKV(outVirtIds);
    }

    @Override
    public R<List<SysEnterpriseExtInvoice>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
        return sysEnterpriseExtInvoiceService.pageQuery(conditions, pageNun, pageSize);

    }

    @Override
    public R<List<SysEnterpriseExtInvoiceExtPathVo>> pageQuery(SysEnterpriseExtInvoiceQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
        return sysEnterpriseExtInvoiceService.pageQuery(queryCriteria, loginSysUser);

    }

    @Override
    public R<List<SysEnterpriseExtInvoiceExtPathVo>> pageQuerySysCliqueXInvoice(SysCliqueXInvoiceCriteria conditions, int pageNun, int pageSize) {

        log.info("\nDubboSysEnterpriseExtInvoiceServiceImpl.pageQuerySysCliqueXInvoice\n -> \n{}\n", AnJsonUtils.toJson(conditions));
        return sysEnterpriseExtInvoiceService.pageQuerySysCliqueXInvoice(conditions, pageNun, pageSize);
    }

    /******************************************  start ******************************************/

    @Override
    public void saveOrUpdate4TaxationAisinoTxtRowParamListByPurchaserTaxpayersIDCode(TaxationAisinoTxtRow param, LoginSysUser loginSysUser) {

        // 如果入参为容 如果税号 纳税人识别码 备注 开企业抬头发票,请填写 纳税人识别号 ,以免影响您报销
        if (null == param || null == param.getPurchaserTaxpayersIDCode()) {
            return;
        }

        SysEnterpriseExtInvoice sysEnterpriseExtInvoice = new SysEnterpriseExtInvoice();

        // 只复写有的
        Dto2EntityConvertUtils.taxationAisinoTxtRow2SysEnterpriseExtInvoiceEntity(param, sysEnterpriseExtInvoice);

        log.info("saveOrUpdate4TaxationAisinoTxtRowParamListByPurchaserTaxpayersIDCode\n{}\n{}", AnJsonUtils.toJson(param), AnJsonUtils.toJson(sysEnterpriseExtInvoice));

        SysEnterpriseExtInvoice checkIsExist = sysEnterpriseExtInvoiceService.getByTaxpayersIDCode(sysEnterpriseExtInvoice.getTaxpayersIDCode());

        if (null == checkIsExist) {
            Dto2EntityConvertUtils.taxationAisinoTxtRow2SysEnterpriseExtInvoiceEntityInit(sysEnterpriseExtInvoice);
            sysEnterpriseExtInvoiceService.addSysEnterpriseExtInvoice(sysEnterpriseExtInvoice, loginSysUser);
        } else {
            sysEnterpriseExtInvoiceService.updateSysEnterpriseExtInvoiceByTaxpayersIDCode(sysEnterpriseExtInvoice);
        }
    }

    @Override
    public void saveOrUpdate4TaxationAisinoTxtRowParamListByPurchaserTaxpayersIDCode(List<TaxationAisinoTxtRow> taxationAisinoTxtRowParamList, LoginSysUser loginSysUser) {

        taxationAisinoTxtRowParamList.forEach(taxationAisinoTxtRowParam -> {
            saveOrUpdate4TaxationAisinoTxtRowParamListByPurchaserTaxpayersIDCode(taxationAisinoTxtRowParam, loginSysUser);
        });
    }

    /****************************************** 缓存 start ******************************************/
    @Override
    public void cacheInit() {

        sysEnterpriseExtInvoiceService.cacheInit();
    }

    @Override
    public void cacheRefresh() {
        sysEnterpriseExtInvoiceService.cacheRefresh();
    }

    @Override
    public void cacheRemove() {
        sysEnterpriseExtInvoiceService.cacheRemove();
    }

    @Override
    public String cacheGet(String hashTable, String keyField) {
        return sysEnterpriseExtInvoiceService.cacheGet(hashTable, keyField);
    }
}
