
package com.jeelowcode.module.biz.service.impl;

import com.jeelowcode.core.framework.mapper.JeeLowCodeSqlMapper;
import com.jeelowcode.core.framework.service.IFrameSqlService;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.plus.SqlHelper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoQueryWrapper;
import com.jeelowcode.framework.plus.core.model.SqlFormatModel;
import com.jeelowcode.module.biz.service.IPlanIssusService;
import com.jeelowcode.module.biz.service.IRiskService;
import com.jeelowcode.service.system.controller.vo.notify.template.NotifyTemplateSendReqVO;
import com.jeelowcode.service.system.service.INotifySendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * demo
 */
@Slf4j
@Service
public class RiskServiceImpl implements IRiskService {

    @Autowired
    private IFrameSqlService sqlService;

    @Resource
    private INotifySendService notifySendService;

    /**
     * 发生消息给整改人
     */
    @Override
    public void sendNotify2Corrective(Long id) {
        // 根据ID查找风险隐患数据
        Map<String, Object>  risk = getRiskById(id);
        NotifyTemplateSendReqVO sendReqVO=new  NotifyTemplateSendReqVO();
        // 构建消息参数
        List<Long> userIdList = new ArrayList<>();
        Map<String, Object> templateParams=new HashMap<>();
        userIdList.add( Long.valueOf(risk.get("corrective_charge_people_id").toString()));
        userIdList.add( Long.valueOf(risk.get("corrective_confirm_people_id").toString()));
        sendReqVO.setUserIdList(userIdList);

        templateParams.put("check_date",risk.get("check_date"));
        templateParams.put("parkName",risk.get("parkName"));
        templateParams.put("check_area",risk.get("check_area"));
        templateParams.put("check_content",risk.get("check_content"));
        templateParams.put("check_problem",risk.get("check_problem"));
        templateParams.put("check_people",risk.get("check_people"));
        sendReqVO.setTemplateCode("risk_corrective_notice");

        sendReqVO.setTemplateParams(templateParams);
        // 发送消息
        notifySendService.sendSingleNotifyToAdmin(sendReqVO.getUserIdList(),
                sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams());

    }

    /**
     * 发送消息给检查人
     */
    @Override
    public void sendNotify2Check(Long id) {
        // 根据ID查找风险隐患数据
        Map<String, Object>  risk = getRiskById(id);
        NotifyTemplateSendReqVO sendReqVO=new  NotifyTemplateSendReqVO();
        // 构建消息参数
        List<Long> userIdList = new ArrayList<>();
        Map<String, Object> templateParams=new HashMap<>();
        userIdList.add( Long.valueOf(risk.get("check_people_id").toString()));
        sendReqVO.setUserIdList(userIdList);

        templateParams.put("billNo",risk.get("billNo"));
        templateParams.put("check_date",risk.get("check_date"));
        templateParams.put("parkName",risk.get("parkName"));
        templateParams.put("check_area",risk.get("check_area"));
        templateParams.put("check_content",risk.get("check_content"));
        templateParams.put("check_problem",risk.get("check_problem"));
        sendReqVO.setTemplateCode("risk_check_notify");

        sendReqVO.setTemplateParams(templateParams);
        // 发送消息
        notifySendService.sendSingleNotifyToAdmin(sendReqVO.getUserIdList(),
                sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams());
    }

    @Override
    public Map<String, Object> getRiskById(long id) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper();
        wrapper.setTableName("lc_risk_hazard_manage");
        wrapper.setWhere(where->{
            where.eq("id",id);
        });
        Map<String, Object> dataMap = sqlService.getDataOneByPlus(wrapper);
        return dataMap;
    }
}
