package com.dxhy.erp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.JxszcCancelEnum;
import com.dxhy.common.enums.JxszcZtEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.erp.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.erp.dao.TdxRecordInvoiceDao;
import com.dxhy.erp.entity.BaseTDxRecordInvoice;
import com.dxhy.erp.entity.BaseTDxRecordInvoiceOut;
import com.dxhy.erp.model.BaseReceiptOutCancelEditReqVO;
import com.dxhy.erp.service.BaseIReceiptInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wangzhikun
 * @Date 2022/12/24 2022/12/24
 */

/**
 * @author peipei.li
 * @date 2019-04-23 进项税模块，底账表单表相关操作
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("baseIReceiptInvoiceService")
public class BaseIReceiptInvoiceServiceImpl extends MpBaseServiceImpl<BaseTDxRecordInvoiceDao, BaseTDxRecordInvoice> implements BaseIReceiptInvoiceService {

    @Resource
    private TdxRecordInvoiceDao tdxRecordInvoiceDao;

    @Override
    public List<BaseTDxRecordInvoice> selectByUuids(String[] uuids) {
        QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(BaseTDxRecordInvoice::getUuid, uuids);
        return super.list(queryWrapper);
    }

    @Override
    public void updateBatch(List<BaseTDxRecordInvoice> condList) {
        for (BaseTDxRecordInvoice entity : condList) {
            LambdaUpdateWrapper<BaseTDxRecordInvoice> updateWrapper =
                    new UpdateWrapper<BaseTDxRecordInvoice>().lambda();
            updateWrapper.eq(BaseTDxRecordInvoice::getId, entity.getId());
            super.update(entity, updateWrapper);
        }
        super.succResult(new JSONObject());
    }

    /**
     * 取消已转出进项税 请求确认
     *
     * @param reqVO 请求参数
     * @return 返回结果
     */
    @Override
    public String cancelOut(BaseReceiptOutCancelEditReqVO reqVO) {
        DynamicContextHolder.push("business"+ DbConstant.BUSINESS_WRITE);

        String[] uuids = reqVO.getPrimaryKey().split(",");
        excuteCancelWrite(uuids);

        return "取消转出成功";
    }

    /**
     * 数据库操作
     *
     * @param uuids 请求参数
     */
    @Transactional(rollbackFor = Exception.class)
    public void excuteCancelWrite(String[] uuids) {
        // 批量将状态设置为 已取消
        updateBatchByUuid(uuids);
        // 批量revert抵账表 转出时候更新的数据
        updatePartInvoiceForCancel(uuids);
    }

    /**
     * 批量将 进项税 明细转为已取消
     *
     * @param uuids 请求参数
     */
    private void updateBatchByUuid(String[] uuids) {
        BaseTDxRecordInvoiceOut entity = new BaseTDxRecordInvoiceOut();
        // 已取消
        entity.setIsCancel(JxszcCancelEnum.YQX.getQxztDm());

        UpdateWrapper<BaseTDxRecordInvoiceOut> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().in(BaseTDxRecordInvoiceOut::getUuid, uuids);
        tdxRecordInvoiceDao.deleteByuuid(uuids);
    }
    /**
     * 取消进项税转出，批量revert抵账表已经更新的字段
     *
     * @param ids 请求参数
     */
    private void updatePartInvoiceForCancel(String[] ids) {
        QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(BaseTDxRecordInvoice::getUuid, ids);
        List<BaseTDxRecordInvoice> list = super.list(queryWrapper);
        for (BaseTDxRecordInvoice condRecord : list) {
            // 转出状态 revert为未转出
            condRecord.setOutStatus(JxszcZtEnum.WZC.getZcztDm());
            // 转出原因
            condRecord.setOutReason(null);
            // 备注
            condRecord.setOutRemark(null);
            // 转出日期
            condRecord.setOutDate(null);
            // 转出人
            condRecord.setOutBy(null);
            // 转出金额
            condRecord.setOutInvoiceAmout(null);
            // 转出税额
            condRecord.setOutTaxAmount(null);
            UpdateWrapper<BaseTDxRecordInvoice> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("out_status",condRecord.getOutStatus());
            updateWrapper.set("out_reason",condRecord.getOutReason());
            updateWrapper.set("out_date",condRecord.getOutDate());
            updateWrapper.set("out_by",condRecord.getOutBy());
            updateWrapper.set("out_invoice_amout",condRecord.getOutInvoiceAmout());
            updateWrapper.set("out_tax_amount",condRecord.getOutTaxAmount());
            updateWrapper.eq("uuid",condRecord.getUuid());
            super.baseMapper.update(null,updateWrapper);

        }
    }
}
