package com.jeeplus.human.enrollment.enrollmentRegistration.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentKeyCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentRegistration;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentRiceCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentRiceCardMapper;
import com.jeeplus.sys.feign.IPostApi;
import com.jeeplus.sys.feign.IRoleApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.PostDTO;
import com.jeeplus.sys.service.dto.RoleDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-10-20 16:50
 */
@Service
@Transactional
public class EnrollmentRiceCardService extends ServiceImpl<EnrollmentRiceCardMapper,EnrollmentRiceCard> {

    @Resource
    private EnrollmentRiceCardMapper riceCardMapper;

    @Resource
    private EnrollmentRegistrationService enrollmentRegistrationService;

    public void deleteByRegistrationId(String id){
        QueryWrapper<EnrollmentRiceCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enrollment_registration_id",id);
        riceCardMapper.delete(queryWrapper);
    }

    public EnrollmentRiceCard findById(String id) throws Exception {
        // 查询基础信息表
        EnrollmentRiceCard info = riceCardMapper.getById(id);
        return info;
    }

    public void updateStatusById(EnrollmentRiceCard dto) {
        riceCardMapper.updateStatusById(dto.getId(), dto.getType());
    }

    public EnrollmentRiceCard saveSecurityCard(EnrollmentRiceCard registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

            EnrollmentRiceCard report = riceCardMapper.getById(registration.getId());
            if (report != null){
                registration.setProcessDefinitionId(report.getProcessDefinitionId());
                registration.setEnrollmentRegistrationId(report.getEnrollmentRegistrationId());
                return update(registration);
            }
        }
        return add(registration);
    }

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentRiceCard add(EnrollmentRiceCard reportData) throws Exception{

        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRiceCard report = new EnrollmentRiceCard();
        BeanUtils.copyProperties(reportData, report);
        report.setCreateById(userDTO.getId());
        report.setCreateTime(new Date());
        report.setUpdateTime(new Date());
        report.setUpdateById(userDTO.getId());
        riceCardMapper.insert(report);

        return report;
    }
    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentRiceCard update(EnrollmentRiceCard reportData) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRiceCard report = new EnrollmentRiceCard();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());
        riceCardMapper.updateById(report);
        if (report.getType().equals("5")){
            //发送短信
            EnrollmentRegistration registration = enrollmentRegistrationService.getById(report.getEnrollmentRegistrationId());
            PostDTO postDTO = SpringUtil.getBean(IPostApi.class).getPostDTOByName("考勤统计人");
            List<UserDTO> listByRoleId = SpringUtil.getBean(IUserApi.class).findListFlowAbleByPostId(postDTO.getId());
            String randomCode = "员工：" + registration.getName() + "已办理入职!";
            if (StringUtils.isNotBlank(listByRoleId.get(0).getMobile())){
                Map<String, Object> stringObjectMap = SpringUtil.getBean(IUserApi.class).sendRandomCodes(listByRoleId.get(0).getMobile(), randomCode);
                String statusCode = (String) stringObjectMap.get("statusCode");
                if (("000000").equals(statusCode)) {
                    System.out.println("短信发送成功!");
                }else if(statusCode.equals("160040")){
                    System.out.println("手机号获取验证码次数已达每日上限!");
                }else{
                    System.out.println("短信发送失败，错误代码：101，请联系管理员!");
                }
            }

        }

        return report;
    }
}
