package com.tofly.feesapi.yhgl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.sfgl.entity.dto.PjchDto;
import com.tofly.feesapi.sfgl.service.YySspjxxService;
import com.tofly.feesapi.yhgl.entity.YyZjzh;
import com.tofly.feesapi.yhgl.entity.dto.YyZjzhUpdate;
import com.tofly.feesapi.yhgl.entity.vo.YyZjzhView;
import com.tofly.feesapi.yhgl.mapper.YyZjzhMapper;
import com.tofly.feesapi.yhgl.service.IYyZjzhService;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class YyZjzhServiceImpl extends ServiceImpl<YyZjzhMapper, YyZjzh> implements IYyZjzhService {

    @Autowired
    private YhKhjbxxService yhKhjbxxService;
    @Autowired
    private YySspjxxService yySspjxxService;

    @Override
    @Transactional
    public String xgyck(YyZjzhUpdate update) {
        String sflsbh = GuidGenerator.getSflsbh();
        if (xgyck(update, false, 1, "2", sflsbh)) {
            return sflsbh;
        }
        return null;
    }

    @Override
    @DSTransactional
    public boolean xgyck(YyZjzhUpdate update, boolean ssbz, int sfxh, String fffs, String sflsbh) {
        String yhbh = update.getYhbh();
        String bz = update.getBz();
        BigDecimal je = update.getJe();
        if (je == null || je.compareTo(BigDecimal.ZERO) == 0) {
            throw new FeesException("金额无效", FeesErrorCode.BUSINESS_ERROR);
        }

        YyZjzh zjzh = createOrGetZjzh(yhbh, je, bz);
        BigDecimal syje = zjzh.getSyje();
        BigDecimal scje = syje.subtract(je);
        BigDecimal bcje = syje;

        if (bcje.compareTo(BigDecimal.ZERO) < 0) {
            throw new FeesException("余额不足", FeesErrorCode.BUSINESS_ERROR);
        }

        if (ssbz) {
            yySspjxxService.addYffsq(yhbh, scje, bcje, je, update.getSfrq(), bz, sfxh, fffs, sflsbh);
        } else {
            // 转存流水账
            yySspjxxService.addZyc(yhbh, scje, bcje, je, update.getSfrq(), bz, sfxh, fffs, sflsbh);
        }

        return this.saveOrUpdate(zjzh);
    }

    @Override
    @DSTransactional
    public boolean dhsfxgyck(YyZjzhUpdate update, String khbm, boolean ssbz, int sfxh, String fffs, String sflsbh) {
        String yhbh = update.getYhbh();
        String bz = update.getBz();
        BigDecimal je = update.getJe();
        if (je == null || je.compareTo(BigDecimal.ZERO) == 0) {
            throw new FeesException("金额无效", FeesErrorCode.BUSINESS_ERROR);
        }

        YyZjzh zjzh = createOrGetZjzh(khbm, je, bz);
        BigDecimal syje = zjzh.getSyje();
        BigDecimal scje = syje.subtract(je);
        BigDecimal bcje = syje;

        if (bcje.compareTo(BigDecimal.ZERO) < 0) {
            throw new FeesException("余额不足", FeesErrorCode.BUSINESS_ERROR);
        }

        if (ssbz) {
            yySspjxxService.addYffsq(yhbh, scje, bcje, je, update.getSfrq(), bz, sfxh, fffs, sflsbh);
        } else {
            // 转存流水账
            yySspjxxService.addZyc(yhbh, scje, bcje, je, update.getSfrq(), bz, sfxh, fffs, sflsbh);
        }

        return this.saveOrUpdate(zjzh);
    }

    private YyZjzh createOrGetZjzh(String yhbh, BigDecimal je, String bz) {
        YyZjzh zjzh = this.getById(yhbh);
        BigDecimal kjje;
        BigDecimal yfje;
        BigDecimal syje;
        BigDecimal nbsyje;
        if (zjzh == null) {
            zjzh = new YyZjzh();
            kjje = BigDecimal.ZERO;
            yfje = BigDecimal.ZERO;
            syje = BigDecimal.ZERO;
            nbsyje = BigDecimal.ZERO;
            zjzh.setYhbh(yhbh);
        } else {
            kjje = zjzh.getKjje();
            yfje = zjzh.getYfje();
            syje = zjzh.getSyje();
            nbsyje = zjzh.getNbsyje();
            kjje = kjje == null ? BigDecimal.ZERO : kjje;
            yfje = yfje == null ? BigDecimal.ZERO : yfje;
            syje = syje == null ? BigDecimal.ZERO : syje;
            nbsyje = nbsyje == null ? BigDecimal.ZERO : nbsyje;
        }

        if (je.compareTo(BigDecimal.ZERO) < 0) {
            if (StringUtil.isNotEmpty(bz) && bz.equals("预存退费")) {
                yfje = yfje.subtract(je.abs());
            } else {
                kjje = kjje.add(je.abs());
            }
            syje = syje.subtract(je.abs());
            nbsyje = nbsyje.subtract(je.abs());

        } else {
            yfje = yfje.add(je);
            syje = syje.add(je);
            nbsyje = nbsyje.add(je);
        }

        zjzh.setKjje(kjje);
        zjzh.setYfje(yfje);
        zjzh.setSyje(syje);
        zjzh.setNbsyje(nbsyje);
        return zjzh;
    }

    @Override
    public YyZjzhView get(String yhbh) {
        return baseMapper.get(yhbh);
    }

    @Override
    public void ycch(PjchDto pjchDto, BigDecimal sszje) {
        String khbm = pjchDto.getKhbm();
        String yhbh = pjchDto.getYhbh();
        if (StrUtil.isNotEmpty(khbm)) {
            baseMapper.ycch(khbm, sszje);
        } else {
            baseMapper.ycch(yhbh, sszje);
        }
    }

    @Override
    public void updateByKfch(BigDecimal bckj, PjchDto pjchDto) {
        String khbm = pjchDto.getKhbm();
        String yhbh = pjchDto.getYhbh();
        if (StrUtil.isNotEmpty(khbm)) {
            baseMapper.kfch(khbm, bckj);
        } else {
            baseMapper.kfch(yhbh, bckj);
        }
    }

    @Override
    public YyZjzh selectByChDto(PjchDto pjchDto) {
        String khbm = pjchDto.getKhbm();
        if (StrUtil.isNotEmpty(khbm)) {
            return getById(khbm);
        }

        String yhbh = pjchDto.getYhbh();
        return getById(yhbh);
    }

}
