package com.jeeplus.business.payment.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.borrow.domain.BorrowMoney;
import com.jeeplus.business.borrow.service.dto.BorrowDTO;
import com.jeeplus.business.payment.domain.Payment;
import com.jeeplus.business.payment.mapper.PaymentMapper;
import com.jeeplus.business.payment.service.dto.PaymentDTO;
import com.jeeplus.business.project.domain.JyProject;
import com.jeeplus.business.project.mapper.JyProjectMapper;
import com.jeeplus.business.reimbursement.domain.JyReimbursementDetailInfoPayment;
import com.jeeplus.business.reimbursement.mapper.JyReimbursementInfoPaymentMapper;
import com.jeeplus.business.workClientInfo.domain.dto.JyWorkClientInfosDto;
import com.jeeplus.business.workClientInfo.mapper.JyWorkClientBankMapper;
import com.jeeplus.business.workClientInfo.mapper.JyWorkClientInfoMapper;
import com.jeeplus.business.workClientInfo.service.JyWorkClientService;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.feign.IWorkAttachmentApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PaymentService {

    @Resource
    private PaymentMapper paymentMapper;

    @Resource
    private JyProjectMapper jyProjectMapper;

    @Resource
    private JyWorkClientService jyWorkClientService;

    @Resource
    private JyReimbursementInfoPaymentMapper jyReimbursementInfoPaymentMapper;


    /*
    列表
     */
    public IPage<PaymentDTO> findPageList(PaymentDTO paymentDTO, Page<PaymentDTO> page) throws Exception {
        QueryWrapper<PaymentDTO> queryWrapper = QueryWrapperGenerator.buildQueryCondition(paymentDTO, PaymentDTO.class);
        queryWrapper.eq("a.del_flag",0);
        //编号
        if (StringUtils.isNotBlank(paymentDTO.getNo())){
            queryWrapper.like("a.no",paymentDTO.getNo());
        }
        //用途
        if (StringUtils.isNotBlank(paymentDTO.getReason())){
            queryWrapper.like("a.reason",paymentDTO.getReason());
        }
        //付款人
        if (StringUtils.isNotBlank(paymentDTO.getPaymentUser())){
            queryWrapper.eq("a.payment_user",paymentDTO.getPaymentUser());
        }
        //付款人部门
        if (StringUtils.isNotBlank(paymentDTO.getPaymentOffice())){
            queryWrapper.eq("so.id",paymentDTO.getPaymentOffice());
        }
        //付款时间
        if (paymentDTO.getCreateDates() != null && paymentDTO.getCreateDates().length > 0) {
            queryWrapper.between("a.payment_time", paymentDTO.getCreateDates()[0], paymentDTO.getCreateDates()[1]);
        }
        //付款金额
        if (paymentDTO.getPaymentAmounts() != null) {
            if (StringUtils.isNotEmpty(paymentDTO.getPaymentAmounts()[0])) {
                queryWrapper.ge("a.payment_lower", Double.parseDouble(paymentDTO.getPaymentAmounts()[0]));
            }
            if (paymentDTO.getPaymentAmounts().length>1 && StringUtils.isNotEmpty(paymentDTO.getPaymentAmounts()[1])) {
                queryWrapper.le("a.payment_lower", Double.parseDouble(paymentDTO.getPaymentAmounts()[1]));
            }
        }
        IPage<PaymentDTO> pageList=paymentMapper.findPageList(queryWrapper,page);
        List<JyReimbursementDetailInfoPayment> list = jyReimbursementInfoPaymentMapper.getList();
        list.stream().forEach(item->{
            pageList.getRecords().stream().forEach(data->{
                if (item.getProjectId().equals(data.getId()) && item.getStatus().equals("5")){
                    Float moneyLower=Float.parseFloat(data.getPaymentLower());
                    Float number=Float.parseFloat(item.getNumber());
                    Float money=moneyLower-number;
                    data.setFinalMoney(money.toString());
                    data.setRefund(item.getNumber());
                }
            });
        });
        return pageList;


    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    public PaymentDTO findById(String id) {
        PaymentDTO paymentDTO=paymentMapper.findById(id);
        //查找项目信息
        if (StringUtils.isNotBlank(paymentDTO.getProjectId())){
            String[] split = paymentDTO.getProjectId().split(",");
            ArrayList<JyProject> projects = new ArrayList<>();
            for (String s : split) {
                JyProject project = jyProjectMapper.getById(s);
                projects.add(project);
            }
            paymentDTO.setProjectDTOList(projects);
        }
        return paymentDTO;
    }

    /*
    新增或修改
     */
    public String save(PaymentDTO paymentDTO) throws Exception {
        if (StringUtils.isNotBlank(paymentDTO.getId())){
            return update(paymentDTO);
        }
        return add(paymentDTO);
    }

    private String add(PaymentDTO paymentDTO) throws Exception {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //生成付款编号
        String No = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNum(userDTO.getCompanyDTO().getId(), PaymentDTO.BIZ_CODE, TokenProvider.getCurrentToken());
        //生成id
        String id = UUID.randomUUID().toString().replace("-", "");

        Payment payment = new Payment();
        //获取项目id
        String projectId="";
        if (ObjectUtils.isNotEmpty(paymentDTO.getProjectDTOList())){
            for (JyProject jyProject : paymentDTO.getProjectDTOList()) {
                projectId+=jyProject.getProjectId()+",";
            }
        }
        payment.setProjectId(projectId);
        payment.setNo(No);
        payment.setId(id);
        payment.setProceedType(paymentDTO.getProceedType());//收款类别
        payment.setProceedOfficeId(paymentDTO.getProceedOfficeId());//收款单位
        payment.setDutyNumber(paymentDTO.getDutyNumber()); //税号
        payment.setAmountPaid(paymentDTO.getAmountPaid());//累计已支付金额
        payment.setReason(paymentDTO.getReason());//用途
        payment.setPaymentLower(paymentDTO.getPaymentLower());//付款金额  小写
        payment.setPayment(paymentDTO.getPayment()); //大写
        payment.setPaymentType(paymentDTO.getPaymentType());//付款方式
        payment.setRemarks(paymentDTO.getRemarks()); //付款摘要
        payment.setPaymentUser(paymentDTO.getPaymentUser()); //付款人
        payment.setPaymentTime(paymentDTO.getPaymentTime());//付款时间
        payment.setOurBank(paymentDTO.getOurBank());//开户银行
        payment.setBankNumber(paymentDTO.getBankNumber());//银行账号
        payment.setStatus(paymentDTO.getStatus());
        payment.setRemarks(paymentDTO.getRemarks());//摘要
        paymentMapper.insert(payment);
        return id;
    }

    private String update(PaymentDTO paymentDTO){
        Payment payment = new Payment();
        //获取项目id
        String projectId="";
        if (ObjectUtils.isNotEmpty(paymentDTO.getProjectDTOList())){
            for (JyProject jyProject : paymentDTO.getProjectDTOList()) {
                projectId+=jyProject.getProjectId()+",";
            }
        }
        payment.setProjectId(projectId);//项目
        payment.setProceedType(paymentDTO.getProceedType());//收款类别
        payment.setProceedOfficeId(paymentDTO.getProceedOfficeId());//收款单位
        payment.setDutyNumber(paymentDTO.getDutyNumber()); //税号
        payment.setAmountPaid(paymentDTO.getAmountPaid());//累计已支付金额
        payment.setReason(paymentDTO.getReason());//用途
        payment.setPaymentLower(paymentDTO.getPaymentLower());//付款金额  小写
        payment.setPayment(paymentDTO.getPayment()); //大写
        payment.setPaymentType(paymentDTO.getPaymentType());//付款方式
        payment.setRemarks(paymentDTO.getRemarks()); //付款摘要
        payment.setPaymentUser(paymentDTO.getPaymentUser()); //付款人
        payment.setPaymentTime(paymentDTO.getPaymentTime());//付款时间
        payment.setOurBank(paymentDTO.getOurBank());//开户银行
        payment.setBankNumber(paymentDTO.getBankNumber());//银行账号
        payment.setId(paymentDTO.getId());
        payment.setStatus(paymentDTO.getStatus());
        payment.setRemarks(paymentDTO.getRemarks());//摘要
        paymentMapper.updateById(payment);

        return paymentDTO.getId();
    }

    /**
     * 根据id修改状态
     * @param paymentDTO
     */
    public void updateStatusById(PaymentDTO paymentDTO) {
        paymentMapper.updateStatusById(paymentDTO.getId(),paymentDTO.getStatus());
    }

    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    public String deleteById(String id) {
        paymentMapper.deleteById(id);
        return "操作成功";
    }

    /**
     * 管理员修改
     * @param paymentDTO
     */
    public void adminEditForm(PaymentDTO paymentDTO) {
        Payment payment = new Payment();
        //获取项目id
        String projectId="";
        if (ObjectUtils.isNotEmpty(paymentDTO.getProjectDTOList())){
            for (JyProject jyProject : paymentDTO.getProjectDTOList()) {
                projectId+=jyProject.getProjectId()+",";
            }
        }
        payment.setProjectId(projectId);//项目
        payment.setProceedType(paymentDTO.getProceedType());//收款类别
        payment.setProceedOfficeId(paymentDTO.getProceedOfficeId());//收款单位
        payment.setDutyNumber(paymentDTO.getDutyNumber()); //税号
        payment.setAmountPaid(paymentDTO.getAmountPaid());//累计已支付金额
        payment.setReason(paymentDTO.getReason());//用途
        payment.setPaymentLower(paymentDTO.getPaymentLower());//付款金额  小写
        payment.setPayment(paymentDTO.getPayment()); //大写
        payment.setPaymentType(paymentDTO.getPaymentType());//付款方式
        payment.setRemarks(paymentDTO.getRemarks()); //付款摘要
        payment.setPaymentUser(paymentDTO.getPaymentUser()); //付款人
        payment.setPaymentTime(paymentDTO.getPaymentTime());//付款时间
        payment.setOurBank(paymentDTO.getOurBank());//开户银行
        payment.setBankNumber(paymentDTO.getBankNumber());//银行账号
        payment.setId(paymentDTO.getId());

        paymentMapper.updateInfoById(payment);
    }

    public IPage<Payment> listByUserId(Payment payment, Page<BorrowDTO> page) throws Exception {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        QueryWrapper<Payment> queryWrapper = QueryWrapperGenerator.buildQueryCondition(payment, Payment.class);
        queryWrapper.eq("a.create_by_id",userDTO.getId());
        queryWrapper.eq("a.status","5");
        queryWrapper.orderByDesc("a.update_time");
        IPage<Payment> moneyList = paymentMapper.listByUserId(queryWrapper,page);
        return moneyList;
    }
}
