package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxComputeD;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxComputeItemD;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxComputeVoucherD;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxContractD;
import com.ruoyi.hywz.mapper.AdsMatHyxtStampTaxComputeVoucherDMapper;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxComputeDService;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxComputeItemDService;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxComputeVoucherDService;
import com.ruoyi.hywz.utils.AmountUtil;
import com.ruoyi.hywz.utils.DateUtil;
import com.ruoyi.hywz.utils.IdGen;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 【印花税】印花税计算单记账凭证 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-07-29 15:59:04
 */
@Service
@AllArgsConstructor
public class AdsMatHyxtStampTaxComputeVoucherDServiceImpl extends ServiceImpl<AdsMatHyxtStampTaxComputeVoucherDMapper,AdsMatHyxtStampTaxComputeVoucherD> implements IAdsMatHyxtStampTaxComputeVoucherDService {

    private final IAdsMatHyxtStampTaxComputeDService adsMatHyxtStampTaxComputeDService;
    private final IAdsMatHyxtStampTaxComputeItemDService adsMatHyxtStampTaxComputeItemDService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateVoucher(String computeId) {
        List<AdsMatHyxtStampTaxComputeVoucherD> list = new ArrayList<>();
        AdsMatHyxtStampTaxComputeD taxComputeD = adsMatHyxtStampTaxComputeDService.getById(computeId);
        BigDecimal convert = AmountUtil.convert(taxComputeD.getAmount());
        if (convert.compareTo(BigDecimal.ZERO) >= 0) {
            List<AdsMatHyxtStampTaxComputeItemD> itemList = adsMatHyxtStampTaxComputeItemDService.getList(computeId);
            String zzr = taxComputeD.getCreateUserName();
            Date date = new Date();
            LoginUser loginUser = SecurityUtils.getLoginUser();

            BigDecimal amount = AmountUtil.convert(taxComputeD.getAmount());
            if (amount.compareTo(new BigDecimal("0")) > 0) {
                // 生成一主、一辅凭证
                this.generateTotal(list, zzr, computeId, taxComputeD.getAmount(), date, loginUser);
            }

            int sort = 2;
            for (AdsMatHyxtStampTaxComputeItemD itemD : itemList) {
                BigDecimal taxNum = AmountUtil.convert(itemD.getTaxNum());
                if (taxNum.compareTo(new BigDecimal("0")) > 0) {
                    this.generateItem(list, zzr, itemD, computeId, sort, date, loginUser);
                    sort = sort + 2;
                }
            }
            if (!list.isEmpty()) {
                this.saveBatch(list);
            }
        }
    }

    @Override
    public Page<AdsMatHyxtStampTaxComputeVoucherD> queryPage(Long pageSize, Long pageNo, AdsMatHyxtStampTaxComputeVoucherD param) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        Page<AdsMatHyxtStampTaxComputeVoucherD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<AdsMatHyxtStampTaxComputeVoucherD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxComputeVoucherD::getIsDeleted, 0);
        if (null != param) {
            queryWrapper.like(StringUtils.isNotBlank(param.getDwOrg()), AdsMatHyxtStampTaxComputeVoucherD::getDwOrg, param.getDwOrg());
            queryWrapper.like(StringUtils.isNotBlank(param.getPzbh()), AdsMatHyxtStampTaxComputeVoucherD::getPzbh, param.getPzbh());
        }

        queryWrapper.orderByDesc(AdsMatHyxtStampTaxComputeVoucherD::getCreateTime);
        queryWrapper.orderByAsc(AdsMatHyxtStampTaxComputeVoucherD::getSort);
        return this.page(page, queryWrapper);
    }

    private void generateTotal(List<AdsMatHyxtStampTaxComputeVoucherD> list, String zzr, String computeId, String amount, Date date, LoginUser loginUser) {
        AdsMatHyxtStampTaxComputeVoucherD v1 = new AdsMatHyxtStampTaxComputeVoucherD();
        v1.setId(IdGen.get32UUID());
        v1.setComputeId(computeId);
        v1.setDw(loginUser.getOrgName());
        v1.setDwOrg(loginUser.getOrgNo());
        v1.setKjzq(DateUtil.getFormat(date, DateUtil.f3));
        v1.setZzrq(DateUtil.getFormat(date, DateUtil.f2));
        v1.setPzbh("转账1");
        v1.setZzr("计提xx印花税");
        v1.setFlzy("计提xx印花税");
        v1.setKmbm("640301");
        v1.setKmmc("税金及附加\\主营业务税金及附加");
        v1.setZfbz("主");
        v1.setTzq("否");
        v1.setZzr(zzr);
        v1.setJzzt("已记账");
        v1.setZhclrq(DateUtil.getFormat(date, DateUtil.f1));
        v1.setBwbbz("人民币");
        v1.setSort(1);

        AdsMatHyxtStampTaxComputeVoucherD v2 = new AdsMatHyxtStampTaxComputeVoucherD();
        v2.setId(IdGen.get32UUID());
        v2.setComputeId(computeId);
        v2.setFlzy("计提xx印花税");
        v2.setZfbz("辅");
        v2.setFzwd("费用明细\\印花税");
        v2.setFzjfje(amount);
        v2.setSort(2);

        list.add(v1);
        list.add(v2);
    }

    private void generateItem(List<AdsMatHyxtStampTaxComputeVoucherD> list, String zzr, AdsMatHyxtStampTaxComputeItemD itemD, String computeId, Integer sort, Date date, LoginUser loginUser) {
        AdsMatHyxtStampTaxComputeVoucherD v1 = new AdsMatHyxtStampTaxComputeVoucherD();
        v1.setId(IdGen.get32UUID());
        v1.setComputeId(computeId);
        v1.setDw(loginUser.getOrgName());
        v1.setDwOrg(loginUser.getOrgNo());
        v1.setKjzq(DateUtil.getFormat(date, DateUtil.f3));
        v1.setZzrq(DateUtil.getFormat(date, DateUtil.f2));
        v1.setPzbh("转账1");
        v1.setZzr("计提xx印花税");
        v1.setFlzy("计提xx印花税");
        v1.setKmbm("222112");
        v1.setKmmc("应交税费\\应交印花税");
        v1.setZfbz("主");
        v1.setTzq("否");
        v1.setZzr(zzr);
        v1.setJzzt("已记账");
        v1.setZhclrq(DateUtil.getFormat(date, DateUtil.f1));
        v1.setBwbbz("人民币");
        v1.setSort(sort+1);

        AdsMatHyxtStampTaxComputeVoucherD v2 = new AdsMatHyxtStampTaxComputeVoucherD();
        String fzwd = "税率（新）:"+itemD.getTaxRate()+"%;税项\\印花税\\"+itemD.getTaxName();
        v2.setId(IdGen.get32UUID());
        v2.setComputeId(computeId);
        v2.setFlzy("计提xx印花税");
        v2.setZfbz("辅");
        v2.setFzwd(fzwd);
        v2.setFzdfje(itemD.getTaxNum());
        v2.setSort(sort+2);

        list.add(v1);
        list.add(v2);
    }
}
