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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.cbgl.entity.YyCblrLs;
import com.tofly.feesapi.cbgl.entity.dto.YyCblrtjFilter;
import com.tofly.feesapi.cbgl.entity.dto.YyCbycmxFilter;
import com.tofly.feesapi.cbgl.entity.vo.CbSltjSelect;
import com.tofly.feesapi.cbgl.entity.vo.CbltjSelect;
import com.tofly.feesapi.cbgl.entity.vo.YyCbycmxSelect;
import com.tofly.feesapi.cbgl.service.YyCblrLsService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.udb.entity.User;
import com.tofly.feesapi.udb.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.cbgl.mapper.YyCblrLsMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.List;

import static com.tofly.feesapi.common.constant.Constants.PERCENT_FORMAT;

@Service("yyCblrLsService")
public class YyCblrLsServiceImpl extends ServiceImpl<YyCblrLsMapper, YyCblrLs> implements YyCblrLsService {

    @Autowired
    private IUserService userService;

    @Override
    public List<CbSltjSelect> cbsltj(YyCblrtjFilter filter) {
        List<CbSltjSelect> cbsltj = this.baseMapper.cbsltj(filter);

        if ("cbry".equals(filter.getTjlb())) {
            List<User> users = userService.list();
            cbsltj.forEach(e -> {
                String cbrybm = e.getTjlb();
                if (cbrybm != null) {
                    users.stream().filter(u -> u.getId().equals(Long.valueOf(cbrybm))).findFirst().ifPresent(user -> e.setTjlb(user.getRealName()));
                }
            });
        }

        if (!cbsltj.isEmpty()) {
            int cbhs = cbsltj.stream().map(e -> e.getCbhs()).reduce(0, (x, y) -> x + y);
            int cbsl = cbsltj.stream().map(e -> e.getCbsl()).reduce(0, (x, y) -> x + y);
            cbsltj.add(new CbSltjSelect("合计", cbhs, cbsl));
        }
        return cbsltj;
    }

    @Override
    public List<CbltjSelect> cbltj(YyCblrtjFilter filter) {
        List<CbltjSelect> cbltj = this.baseMapper.cbltj(filter);
        List<User> users = userService.list();
        cbltj.forEach(e -> {
            if ("cbb".equals(filter.getTjlb())) {
                String cbrybm = e.getCby();
                if (cbrybm != null) {
                    users.stream().filter(u -> u.getId().toString().equals(cbrybm)).findFirst()
                            .ifPresent(user -> e.setCby(user.getRealName()));
                }
            }
            if ("cbry".equals(filter.getTjlb())) {
                String cbrybm = e.getTjlb();
                if (cbrybm != null) {
                    users.stream().filter(u -> u.getId().toString().equals(cbrybm)).findFirst()
                            .ifPresent(user -> e.setTjlb(user.getRealName()));
                }
            }
            Integer ycbs = e.getYcbs();
            Integer scbs = e.getScbs();
            e.setWcbs(ycbs - scbs);
            BigDecimal cbl = new BigDecimal(scbs * 100).divide(new BigDecimal(ycbs), 2, RoundingMode.DOWN);
            e.setCbl(PERCENT_FORMAT.format(cbl));
        });
        if (!cbltj.isEmpty()) {
            int ycbs = cbltj.stream().map(e -> e.getYcbs()).reduce(0, (x, y) -> x + y);
            int scbs = cbltj.stream().map(e -> e.getScbs()).reduce(0, (x, y) -> x + y);
            int scsl = cbltj.stream().map(e -> e.getScsl()).reduce(0, (x, y) -> x + y);
            int wcbs = cbltj.stream().map(e -> e.getWcbs()).reduce(0, (x, y) -> x + y);
            BigDecimal cbl = new BigDecimal(scbs * 100).divide(new BigDecimal(ycbs), 2, RoundingMode.DOWN);
            cbltj.add(new CbltjSelect("合计", ycbs, scbs, scsl, wcbs, PERCENT_FORMAT.format(cbl), ""));
        }
        return cbltj;
    }

    @Override
    public PageResult<YyCbycmxSelect> cbycmxPage(PageDto pageDto, YyCbycmxFilter filter) {
        String cwrq = filter.getCwrq();
        if (StringUtils.isBlank(cwrq)) throw new FeesException("财务日期为空", FeesErrorCode.BUSINESS_ERROR);
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyyMM")
                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                .toFormatter();
        LocalDate localDate = LocalDate.parse(cwrq, formatter);
        String cwrq1 = localDate.plusMonths(-1).format(formatter);
        String cwrq2 = localDate.plusMonths(-2).format(formatter);
        String cwrq3 = localDate.plusMonths(-3).format(formatter);
        PageDto<YyCbycmxSelect> cbycmx = this.baseMapper.cbycmxPage(pageDto, filter, cwrq1, cwrq2, cwrq3);
        return cbycmx.toVo();
    }

    @Override
    public List<YyCbycmxSelect> cbycmx(YyCbycmxFilter filter) {
        String cwrq = filter.getCwrq();
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyyMM")
                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                .toFormatter();
        LocalDate localDate = LocalDate.parse(cwrq, formatter);
        String cwrq1 = localDate.plusMonths(-1).format(formatter);
        String cwrq2 = localDate.plusMonths(-2).format(formatter);
        String cwrq3 = localDate.plusMonths(-3).format(formatter);
        return this.baseMapper.cbycmx(filter, cwrq1, cwrq2, cwrq3);
    }
}
