package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.InvestigateDispositionTable;
import com.ruoyi.system.domain.InvestigateUser;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.InvestigateDispositionTableMapper;
import com.ruoyi.system.mapper.InvestigateUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InvestigateTableMapper;
import com.ruoyi.system.domain.InvestigateTable;
import com.ruoyi.system.service.IInvestigateTableService;

/**
 * 考察主Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-10
 */
@Service
public class InvestigateTableServiceImpl implements IInvestigateTableService {
    @Autowired
    private InvestigateTableMapper investigateTableMapper;
    @Autowired
    private InvestigateDispositionTableMapper investigateDispositionTableMapper;
    @Autowired
    private InvestigateUserMapper investigateUserMapper;

    /**
     * 查询考察主
     *
     * @param id 考察主主键
     * @return 考察主
     */
    @Override
    public InvestigateTable selectInvestigateTableById(Long id) {
        return investigateTableMapper.selectInvestigateTableById(id);
    }

    /**
     * 查询考察主列表
     *
     * @param investigateTable 考察主
     * @return 考察主
     */
    @Override
    public List<InvestigateTable> selectInvestigateTableList(InvestigateTable investigateTable) {
        return investigateTableMapper.selectInvestigateTableList(investigateTable);
    }

    /**
     * 新增考察主
     *
     * @param investigateTable 考察主
     * @return 结果
     */
    @Override
    public int insertInvestigateTable(InvestigateTable investigateTable) {
        investigateTable.setCreateTime(DateUtils.getNowDate());
        //新增考察配置与考察主表关联
        //insertInvestigateDispositionTable(investigateTable);
        List<InvestigateUser> investigateUserList = investigateTable.getInvestigateUserList();
        investigateTable.setCipher(getCode(4));
        int i = investigateTableMapper.insertInvestigateTable(investigateTable);
        if (investigateUserList != null) {
            for (InvestigateUser investigateUser : investigateUserList) {
                investigateUser.setInvestigateId(investigateTable.getInvestigateTableId());
            }
            investigateUserMapper.batchDispositionUser(investigateUserList);
        }
        return i;
    }

    //生成X位验证码
    public static String getCode(Integer num) {
        String[] codes = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int j = (int) (Math.random() * 10);
            if (j <= 0) {
                j = 1;
            }
            code.append(codes[j - 1]);

        }
        return code.toString();
    }

    /**
     * 新增考察配置与考察主表关联
     *
     * @param investigateTable 考察主表对象
     */
    public void insertInvestigateDispositionTable(InvestigateTable investigateTable) {
        this.insertInvestigateDispositionTable(investigateTable.getInvestigateTableId(), investigateTable.getInvestigateDispositionTableIds());
    }

    /**
     * 新增考察配置与考察主表关联
     *
     * @param investigateTableId             察主表ID
     * @param investigateDispositionTableIds 考察配置组
     */
    public void insertInvestigateDispositionTable(Long investigateTableId, Long[] investigateDispositionTableIds) {
        if (StringUtils.isNotEmpty(investigateDispositionTableIds)) {
            // 新增考察配置与考察主表关联
            List<InvestigateDispositionTable> list = new ArrayList<InvestigateDispositionTable>(investigateDispositionTableIds.length);
            for (Long dispositionId : investigateDispositionTableIds) {
                InvestigateDispositionTable investigateDispositionTable = new InvestigateDispositionTable();
                investigateDispositionTable.setInvestigateTableId(investigateTableId);
                investigateDispositionTable.setInvestigateDispositionId(dispositionId);
                list.add(investigateDispositionTable);
            }
            investigateDispositionTableMapper.batchDispositionTable(list);
        }
    }

    /**
     * 修改考察主
     *
     * @param investigateTable 考察主
     * @return 结果
     */
    @Override
    public int updateInvestigateTable(InvestigateTable investigateTable) {
        investigateTable.setUpdateTime(DateUtils.getNowDate());
        //删除原先的考察人员数据
        Long investigateTableId = investigateTable.getInvestigateTableId();
        investigateUserMapper.deleteInvestigateUserByInvestigateId(investigateTableId);
        List<InvestigateUser> investigateUserList = investigateTable.getInvestigateUserList();
        if (investigateUserList != null) {
            for (InvestigateUser investigateUser : investigateUserList) {
                investigateUser.setInvestigateId(investigateTable.getInvestigateTableId());
            }
            investigateUserMapper.batchDispositionUser(investigateUserList);
        }
        return investigateTableMapper.updateInvestigateTable(investigateTable);
    }

    /**
     * 批量删除考察主
     *
     * @param ids 需要删除的考察主主键
     * @return 结果
     */
    @Override
    public int deleteInvestigateTableByIds(Long[] ids) {
        return investigateTableMapper.deleteInvestigateTableByIds(ids);
    }

    /**
     * 删除考察主信息
     *
     * @param id 考察主主键
     * @return 结果
     */
    @Override
    public int deleteInvestigateTableById(Long id) {
        return investigateTableMapper.deleteInvestigateTableById(id);
    }
}
