package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SendSmsUtils;
import com.ruoyi.system.domain.InvestigateTable;
import com.ruoyi.system.mapper.InvestigateTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InvestigateUserMapper;
import com.ruoyi.system.domain.InvestigateUser;
import com.ruoyi.system.service.IInvestigateUserService;

/**
 * 考察人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-10
 */
@Service
public class InvestigateUserServiceImpl implements IInvestigateUserService {
    @Autowired
    private InvestigateUserMapper investigateUserMapper;

    @Autowired
    private InvestigateTableMapper investigateTableMapper;

    /**
     * 查询考察人员信息
     *
     * @param id 考察人员信息主键
     * @return 考察人员信息
     */
    @Override
    public InvestigateUser selectInvestigateUserById(Long id) {
        return investigateUserMapper.selectInvestigateUserById(id);
    }

    /**
     * 查询考察人员信息列表
     *
     * @param investigateUser 考察人员信息
     * @return 考察人员信息
     */
    @Override
    public List<InvestigateUser> selectInvestigateUserList(InvestigateUser investigateUser) {
        return investigateUserMapper.selectInvestigateUserList(investigateUser);
    }

    /**
     * 新增考察人员信息
     *
     * @param investigateUser 考察人员信息
     * @return 结果
     */
    @Override
    public int insertInvestigateUser(InvestigateUser investigateUser) {
        investigateUser.setCreateTime(DateUtils.getNowDate());
        return investigateUserMapper.insertInvestigateUser(investigateUser);
    }

    /**
     * 修改考察人员信息
     *
     * @param investigateUser 考察人员信息
     * @return 结果
     */
    @Override
    public int updateInvestigateUser(InvestigateUser investigateUser) {
        investigateUser.setUpdateTime(DateUtils.getNowDate());
        return investigateUserMapper.updateInvestigateUser(investigateUser);
    }

    /**
     * 批量删除考察人员信息
     *
     * @param ids 需要删除的考察人员信息主键
     * @return 结果
     */
    @Override
    public int deleteInvestigateUserByIds(Long[] ids) {
        return investigateUserMapper.deleteInvestigateUserByIds(ids);
    }

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

    /**
     * @param investigateUser
     * @return
     */
    @Override
    public AjaxResult sendSmsInvestigateTable(InvestigateUser investigateUser) {

        List<InvestigateUser> investigateUsers = investigateUserMapper.selectInvestigateUserList(investigateUser);
        if (investigateUsers != null && investigateUsers.size() > 0) {
            InvestigateTable investigateTable = investigateTableMapper.selectInvestigateTableById(investigateUser.getInvestigateId());
            /**
             * 密码
             */
            JSONObject jsonObject = new JSONObject();
            String cipher = investigateTable.getCipher();
            jsonObject.put("code", cipher);
            //查询考察密码
            String phonenumber = investigateUser.getPhonenumber();
            String templateCode = "SMS_463627835";
            String code = SendSmsUtils.sendSms(phonenumber, templateCode, jsonObject.toString());
            System.out.println("发送短信回执"+code);
            return AjaxResult.success();
        }
        return AjaxResult.error("无权限查看");
    }
}
