package com.jpsoft.shinestar.mq.listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jpsoft.shinestar.modules.base.entity.ProjectInfo;
import com.jpsoft.shinestar.modules.base.service.ProjectInfoService;
import com.jpsoft.shinestar.modules.common.utils.AESUtil;
import com.jpsoft.shinestar.modules.realname.dto.WorkerAttendanceDTO;
import com.jpsoft.shinestar.modules.realname.http.RealnameSysRequest;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
@RabbitListener(queues = "syncWorkerAttendenceQueue",concurrency = "1")
public class RealnameSysListener {
    @Autowired
    private ProjectInfoService projectInfoService;

    @Value("${realname.selectUrl}")
    private String selectUrl;

    @Value("${realname.uploadUrl}")
    private String uploadUrl;

    @Autowired
    private SysLogService sysLogService;

    @RabbitHandler
    public void process(WorkerAttendanceDTO dto) {
        RealnameSysRequest request = new RealnameSysRequest();

        SysLog sysLog = new SysLog();
        sysLog.setPointcut(dto.getIdNumber());
        sysLog.setUrl("WorkerAttendance.Add");

        try {
            ProjectInfo projectInfo = projectInfoService.findOneByProjectCode(dto.getProjectCode());

            if(projectInfo!=null) {
                //查询职工合同编号
                JSONObject projectWorker = queryProjectWorker(projectInfo, dto.getIdNumber());

                if (projectWorker != null) {
                    dto.setIdNumber(AESUtil.encrypt(dto.getIdNumber(), projectInfo.getAppSecret()));
                    dto.setContractType(projectWorker.getString("contracttype"));

                    List<WorkerAttendanceDTO> dtoList = new ArrayList<>();
                    dtoList.add(dto);
                    String jsonStr = JSONObject.toJSONString(dtoList);

                    JSONObject response = request.sendPost(projectInfo.getAppId(), projectInfo.getAppSecret(), uploadUrl, "WorkerAttendance.Add", jsonStr);
                    String remark = response.getString("message");

                    sysLog.setData(jsonStr);
                    sysLog.setRemark(remark);
                } else {
                    sysLog.setRemark("项目：" + projectInfo.getProjectCode() + "不存在身份证号：" + dto.getIdNumber());
                }
            }
            else{
                sysLog.setRemark("项目编码不存在：" + dto.getProjectCode());
            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
            sysLog.setRemark(ex.getMessage());
        }

        sysLog.setCreateTime(new Date());
        sysLogService.insert(sysLog);
    }

    public JSONObject queryProjectWorker(ProjectInfo projectInfo,String idNumber){
        Map<String,Object> map = new HashMap<>();
        map.put("PROJECTCODE",projectInfo.getProjectCode());
        map.put("IDNUMBER",idNumber);
        map.put("PAGE",1);
        map.put("ROWS",10);

        RealnameSysRequest request = new RealnameSysRequest();
        String jsonStr = JSONObject.toJSONString(map);

        JSONObject response = request.sendPost(projectInfo.getAppId(),projectInfo.getAppSecret(),selectUrl,"ProjectWorker.Sel",jsonStr);

        JSONArray jsonArray = response.getJSONObject("data").getJSONArray("data");

        if(jsonArray.size()>0){
            return jsonArray.getJSONObject(0);
        }
        else{
            return null;
        }
    }
}
