package com.fjhx.account.service.loan.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.account.entity.account.enums.AccountRunningWaterEnum;
import com.fjhx.account.entity.account.po.AccountManagement;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.entity.loan.dto.LoanInfoDto;
import com.fjhx.account.entity.loan.dto.LoanInfoSelectDto;
import com.fjhx.account.entity.loan.po.LoanInfo;
import com.fjhx.account.entity.loan.vo.LoanInfoVo;
import com.fjhx.account.mapper.loan.LoanInfoMapper;
import com.fjhx.account.service.account.AccountManagementService;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.account.service.loan.LoanInfoService;
import com.fjhx.common.entity.corporation.po.Corporation;
import com.fjhx.common.service.corporation.CorporationService;
import com.fjhx.common.utils.Assert;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 借款信息 服务实现类
 * </p>
 *
 * @author
 * @since 2023-09-15
 */
@Service
public class LoanInfoServiceImpl extends ServiceImpl<LoanInfoMapper, LoanInfo> implements LoanInfoService {

    @Autowired
    private CorporationService corporationService;
    @Autowired
    private AccountManagementService accountManagementService;
    @Autowired
    private AccountRunningWaterService accountRunningWaterService;

    @Override
    public Page<LoanInfoVo> getPage(LoanInfoSelectDto dto) {
        IWrapper<LoanInfo> wrapper = getWrapper();
        //归属公司
        wrapper.eq(LoanInfo::getCorporationId, dto.getCorporationId());
        //到款状态
        wrapper.eq(LoanInfo::getRepaymentStatus, dto.getRepaymentStatus());
        //关键字
        wrapper.keyword(dto.getKeyword(),
                new SqlField(LoanInfo::getLoanUserName),
                new SqlField(LoanInfo::getAmount),
                new SqlField(LoanInfo::getLoanTime)
        );
        //排序 借款时间>创建时间>id
        wrapper.orderByDesc("li", LoanInfo::getLoanTime);
        wrapper.orderByDesc("li", LoanInfo::getCreateTime);
        wrapper.orderByDesc("li", LoanInfo::getId);
        Page<LoanInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<LoanInfoVo> records = page.getRecords();
        //赋值归属公司名称
        corporationService.attributeAssign(records, LoanInfoVo::getCorporationId, (item, corporation) -> {
            item.setCorporationName(corporation.getName());
        });
        //赋值创建人名称
        UserUtil.assignmentNickName(records, LoanInfoVo::getCreateUser, LoanInfoVo::setCreateUserName);
        return page;
    }

    /**
     * 借款人列表
     */
    @Override
    public List<LoanInfo> getLoanUserList() {
        List<LoanInfo> list = this.list(q -> q
                .select(LoanInfo::getLoanUserName)
                .groupBy(LoanInfo::getLoanUserName)
                .isNotNull(LoanInfo::getLoanUserName)
                .eq(LoanInfo::getCreateUser, SecurityUtils.getUserId())
        );
        return list;
    }

    @Override
    public LoanInfoVo detail(Long id) {
        Assert.notEmpty(id, "借款id不能为空");
        LoanInfo LoanInfo = this.getById(id);
        LoanInfoVo result = BeanUtil.toBean(LoanInfo, LoanInfoVo.class);
        //赋值归属公司名称
        Corporation corporation = corporationService.getById(result.getCorporationId());
        if (ObjectUtil.isNotEmpty(corporation)) {
            result.setCorporationName(corporation.getName());
        }
        //赋值付款账号名称
        AccountManagement accountManagement = accountManagementService.getById(result.getLoanAccountId());
        if (ObjectUtil.isNotEmpty(accountManagement)) {
            result.setLoanAccountName(accountManagement.getName());
        }
        return result;
    }

    @Override
    @DSTransactional
    public void add(LoanInfoDto loanInfoDto) {
        loanInfoDto.setRepaymentStatus(0);
        this.save(loanInfoDto);
        //创建支出流水
        AccountRunningWater accountRunningWater = new AccountRunningWater();
        accountRunningWater.setStatus("20");
        accountRunningWater.setType(AccountRunningWaterEnum.LOAN.getKey());
        accountRunningWater.setRemarks(loanInfoDto.getRemarks());
        accountRunningWater.setTransactionTime(loanInfoDto.getLoanTime());
        accountRunningWater.setAccountManagementId(loanInfoDto.getLoanAccountId());
        accountRunningWater.setAmount(loanInfoDto.getAmount());
        Assert.notEmpty(loanInfoDto.getCurrency(), "借款币种不能为空");
        accountRunningWater.setCurrency(loanInfoDto.getCurrency());
        accountRunningWater.setBusinessId(loanInfoDto.getId());
        //操作余额
        accountRunningWaterService.changeRemainder(accountRunningWater);
        accountRunningWaterService.save(accountRunningWater);
    }

}
