package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DkFj;
import com.ruoyi.system.domain.DksqLcjl;
import com.ruoyi.system.mapper.DkFjMapper;
import com.ruoyi.system.mapper.DksqLcjlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DksqMapper;
import com.ruoyi.system.domain.Dksq;
import com.ruoyi.system.service.IDksqService;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import static com.ruoyi.common.constant.Constants.*;

/**
 * 贷款申请_主Service业务层处理
 *
 * @author boman
 * @date 2023-06-15
 */
@Service
public class DksqServiceImpl implements IDksqService {
    @Autowired
    private DksqMapper dksqMapper;

    @Autowired
    private DksqLcjlMapper dksqLcjlMapper;
    @Autowired
    private DkFjMapper dkFjMapper;


    /**
     * 查询贷款申请_主
     *
     * @param id 贷款申请_主主键
     * @return 贷款申请_主
     */
    @Override
    public Dksq selectDksqById(Long id) {
        Dksq dksq = dksqMapper.selectDksqById(id);
        //查询附件
        DkFj dkFj = new DkFj();
        dkFj.setDkId(id);
        List<DkFj> dkFjs = dkFjMapper.selectDkFjList(dkFj);
        dksq.setDkFjList(dkFjs);
        //查询流程
        DksqLcjl dksqLcjl = new DksqLcjl();
        dksqLcjl.setDksqId(id);
        List<DksqLcjl> dksqLcjls = dksqLcjlMapper.selectDksqLcjlList(dksqLcjl);
        dksq.setDksqLcjlList(dksqLcjls);
        return dksq;
    }

    /**
     * 查询贷款申请_主列表
     *
     * @param dksq 贷款申请_主
     * @return 贷款申请_主
     */
    @Override
    public List<Dksq> selectDksqList(Dksq dksq) {
        //判断用户角色 如果是银行角色
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0){
            for (SysRole role : roles) {
                 if ("bank".equals(role.getRoleKey())){
                     //查询指派给自己的
                    dksq.setFdBankId(SecurityUtils.getLoginUser().getDeptId());
                    break;
                }
            }
        }
        List<Dksq> dksqs = dksqMapper.selectDksqList(dksq);
        return dksqs;
    }

    /**
     * 新增贷款申请_主
     *
     * @param dksq 贷款申请_主
     * @return 结果
     */
    @Override
    public int insertDksq(Dksq dksq) {
        dksq.setCreateBy(SecurityUtils.getUsername());
        dksq.setCreateTime(DateUtils.getNowDate());
        int i = dksqMapper.insertDksq(dksq);
        //插入附件表
        List<DkFj> dkFjList = dksq.getDkFjList();
        if (dkFjList != null && dkFjList.size() > 0){
            for (DkFj dkFj : dkFjList) {
                dkFj.setDkId(dksq.getId());
                dkFjMapper.insertDkFj(dkFj);
            }
        }
        //插入流程记录表
        DksqLcjl dksqLcjl = new DksqLcjl();
        dksqLcjl.setDksqId(dksq.getId());
        dksqLcjl.setType(ONE);
        dksqLcjl.setResult(TWO);
        dksqLcjl.setCreateBy(SecurityUtils.getUsername());
        dksqLcjlMapper.insertDksqLcjl(dksqLcjl);

        return i;
    }

    /**
     * 修改贷款申请_主
     *
     * @param dksq 贷款申请_主
     * @return 结果
     */
    @Override
    public int updateDksq(Dksq dksq) {
        //判断什么时候能修改
        String result = dksq.getResult();
        String type = dksq.getType();
        int i = 0;
        //只有人社局审核不通过,才能让申请用户修改
        if (TWO.equals(type) && THR.equals(result)) {
            dksq.setUpdateTime(DateUtils.getNowDate());
            //修改成提交申请/通过
            dksq.setType(ONE);
            dksq.setResult(TWO);
            i = dksqMapper.updateDksq(dksq);

            //插入流程记录表
            DksqLcjl dksqLcjl = new DksqLcjl();
            dksqLcjl.setDksqId(dksq.getId());
            dksqLcjl.setType(ONE);
            dksqLcjl.setResult(TWO);
            dksqLcjl.setCreateBy(SecurityUtils.getUsername());
            dksqLcjlMapper.insertDksqLcjl(dksqLcjl);
            //删除原先的附件,上传新的附件
            dkFjMapper.deleteDkFjByDkId(dksq.getId());
            List<DkFj> dkFjList = dksq.getDkFjList();
            if (dkFjList != null && dkFjList.size() > 0){
                for (DkFj dkFj : dkFjList) {
                    dkFj.setDkId(dksq.getId());
                    dkFjMapper.insertDkFj(dkFj);
                }
            }
        }
        return i;
    }

    /**
     * 批量删除贷款申请_主
     *
     * @param ids 需要删除的贷款申请_主主键
     * @return 结果
     */
    @Override
    public int deleteDksqByIds(Long[] ids) {
        return dksqMapper.deleteDksqByIds(ids);
    }

    /**
     * 删除贷款申请_主信息
     *
     * @param id 贷款申请_主主键
     * @return 结果
     */
    @Override
    public int deleteDksqById(Long id) {
        return dksqMapper.deleteDksqById(id);
    }

    /**
     * 根据申请人id查询申请信息
     * @param idCard
     * @return
     */
    @Override
    public List<Dksq> selectDksqByIdCard(String idCard) {
        return dksqMapper.selectDksqByIdCard(idCard);
    }
}
