package vip.xiaonuo.money.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.money.entity.MoneyBorrowWarning;
import vip.xiaonuo.money.entity.vo.MoneyBankNameVo;
import vip.xiaonuo.money.entity.vo.MoneyBorrowPriceVo;
import vip.xiaonuo.money.mapper.MoneyBorrowWarningMapper;
import vip.xiaonuo.money.param.MoneyBorrowDetailParam;
import vip.xiaonuo.money.param.MoneyBorrowParam;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyBorrowWarningService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 财务临近还款期预警 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-20
 */
@Service
public class MoneyBorrowWarningServiceImpl extends ServiceImpl<MoneyBorrowWarningMapper, MoneyBorrowWarning> implements IMoneyBorrowWarningService {

    @Autowired
    private MoneyBorrowWarningMapper borrowWarningMapper;

    @Override
    public List<MoneyBorrowWarning> listBorrowWarning(MoneyInOutParam moneyInOutParam) {
        String orgId = moneyInOutParam.getOrgId();
        Date date = new Date();
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId).eq("is_repayment",0);
        List<MoneyBorrowWarning> list = new ArrayList<>();
        List<MoneyBorrowWarning> borrowWarnings = borrowWarningMapper.selectList(queryWrapper);
        if (borrowWarnings.size() != 0) {
            for (MoneyBorrowWarning borrowWarning : borrowWarnings) {
                Date endTime = borrowWarning.getEndTime();
                if (endTime.compareTo(date) < 0) {
                    System.out.println("超过还款日");
                } else {
                    long l = endTime.getTime() - date.getTime();
                    long l1 = l / (24 * 60 * 60 * 1000);
                    if (l1 < 16) {
                        list.add(borrowWarning);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public JSONObject getMoneyCount(MoneyBorrowParam borrowParam) {
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(borrowParam.getOrgId()), "org_id", borrowParam.getOrgId());
        queryWrapper.eq(StringUtils.isNotBlank(borrowParam.getBankName()), "bank_name", borrowParam.getBankName());
        queryWrapper.select(" IFNULL(sum(borrow_price),'0') borrowPrice");
        MoneyBorrowWarning borrowWarning = borrowWarningMapper.selectOne(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        BigDecimal bg = new BigDecimal(borrowWarning.getBorrowPrice());
        bg = bg.divide(math, 2, RoundingMode.HALF_UP);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("money", bg.toString());
        return jsonObject;
    }

    @Override
    public List<MoneyBankNameVo> bankRank(MoneyBorrowParam borrowParam) {
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(borrowParam.getOrgId()), "org_id", borrowParam.getOrgId());
        List<MoneyBankNameVo> bankRank = borrowWarningMapper.getBankRank(queryWrapper);
        return bankRank;
    }

    @Override
    public List<MoneyBorrowPriceVo> borrowMoney(MoneyBorrowParam borrowParam) {
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(borrowParam.getOrgId()), "org_id", borrowParam.getOrgId());
        queryWrapper.eq(StringUtils.isNotBlank(borrowParam.getBankName()), "bank_name", borrowParam.getBankName());
        List<MoneyBorrowPriceVo> list = new ArrayList<>();
        List<MoneyBorrowPriceVo> borrowPrice = borrowWarningMapper.getBorrowPrice(queryWrapper);
        for (int i = 1; i < 13; i++) {
            MoneyBorrowPriceVo vo = new MoneyBorrowPriceVo();
            if (i < 10) {
                vo.setBorrowTime("0"+i+"月");
                vo.setBorrowPrice("0");
            } else {
                vo.setBorrowTime(i+"月");
                vo.setBorrowPrice("0");
            }
            for (MoneyBorrowPriceVo priceVo : borrowPrice) {
                if (i == Integer.parseInt(priceVo.getBorrowTime().substring(5, 7))) {
                    String price = priceVo.getBorrowPrice();

                    vo.setBorrowPrice(priceVo.getBorrowPrice());
                }
            }
            list.add(vo);
        }
        return list;
    }

    @Override
    public Page<MoneyBorrowWarning> listBorrowDetail(MoneyBorrowDetailParam detailParam) {
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(detailParam.getOrgId()), "org_id", detailParam.getOrgId());
        queryWrapper.eq(StringUtils.isNotBlank(detailParam.getBankName()), "bank_name", detailParam.getBankName());
        Page page = new Page<MoneyBorrowWarning>(detailParam.getPageNum(), detailParam.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public CommonResult add(MoneyBorrowWarning moneyBorrowWarning) {
        if (moneyBorrowWarning.getOrgId() == null || "".equals(moneyBorrowWarning.getOrgId())) {
            return CommonResult.error("公司不能为空");
        }
        borrowWarningMapper.insert(moneyBorrowWarning);
        return CommonResult.ok();
    }

    @Override
    public void update(MoneyBorrowWarning moneyBorrowWarning) {
        borrowWarningMapper.updateById(moneyBorrowWarning);
    }

    @Override
    public void remove(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        borrowWarningMapper.deleteById(id);
    }

    @Override
    public List<MoneyBorrowWarning> listBank(Map<String, String> paramsMap) {
        QueryWrapper<MoneyBorrowWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("bank_name");
        return borrowWarningMapper.selectList(queryWrapper);
    }

}
