package com.weaver.seconddev.chapanda.beisen.controller;

import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.ebuilder.form.client.entity.data.EBDataChangeResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weaver.seconddev.chapanda.beisen.dao.EmployeePointsDao;
import com.weaver.seconddev.chapanda.beisen.service.ConversionResignService;
import com.weaver.seconddev.chapanda.beisen.util.CommonUtils;
import com.weaver.seconddev.chapanda.beisen.util.EbuilderUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/***
 *
 *  积分计算 POC开发的
 */
@RestController
@RequestMapping("/api/secondev/cbdhr/points")
public class EmployeePointsController {

    private final Logger log = LoggerFactory.getLogger(EmployeePointsController.class);

    @Autowired
    ConversionResignService conversionResignService;

    @Autowired
    CommonUtils CommonUtils;

    @Autowired
    EmployeePointsDao employeePointsDao;

    @Autowired
    EbuilderUtils ebuilderUtils;

    @WeaPermission(publicPermission = true)
    @GetMapping("/getpoints")
    public WeaResult<Object> resignConversion(HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            List<Map<String, Object>> interviewerList = employeePointsDao.queryInterviewerList();
            Map<String,Object> recordMap = new HashMap<String,Object>();
            log.error("interviewerList:"+interviewerList.size());
            recordMap.put("interviewerList",interviewerList.size());
            recordList.add(recordMap);

            for(int i=0;i<interviewerList.size();i++){

                recordMap = new HashMap<String,Object>();

                /**
                 * 招聘阶段
                 */
                String stage_id = String.valueOf(interviewerList.get(i).get("stage_id"));
                /**
                 * 人才ID
                 */
                String talent_id = String.valueOf(interviewerList.get(i).get("talent_id"));
                /***
                 * 推荐人ID
                 */
                String referrer_id = String.valueOf(interviewerList.get(i).get("referrer_id"));


                log.error("stage_id:"+stage_id);
                log.error("talent_id:"+talent_id);
                log.error("referrer_id:"+referrer_id);

                recordMap.put("stage_id",stage_id);
                recordMap.put("talent_id",talent_id);
                recordMap.put("referrer_id",referrer_id);

                int points = 0;

                /***
                 * 出试
                 */
                if("937153853968544649".equals(stage_id)){
                    points = 100;

                }else if("937153853968544650".equals(stage_id)){
                    /***
                     * 部门筛选
                     */
                    points = 200;
                }else if("937153853968544653".equals(stage_id)){
                    /***
                     * 待入职
                     */
                    points = 500;
                }
                log.error("points:{}",points);

                recordMap.put("points",points);

                if(points > 0){
                    Map<String,Object> pointsMap = employeePointsDao.queryPointsByEmployeeId(referrer_id);
                    /***
                     *  积分主表id
                     */
                    String mainid = String.valueOf(pointsMap.get("id"));
                    if("null".equals(mainid) || mainid == null){
                        mainid = "";
                    }
                    /**
                     * 积分主表form_data_id
                     */
                    String pointsFormDataId= String.valueOf(pointsMap.get("form_data_id"));
                    if("null".equals(pointsFormDataId) || pointsFormDataId == null){
                        pointsFormDataId = "";
                    }

                    log.error("mainid:{}",mainid);
                    log.error("pointsFormDataId:{}",pointsFormDataId);

                    recordMap.put("mainid",mainid);
                    recordMap.put("pointsFormDataId",pointsFormDataId);

                    EBDataChangeResult ebDataChangeResult = null;
                    if(StringUtils.isNotBlank(mainid)){

                        recordMap.put("num","1");
                        String detailId = employeePointsDao.queryPointsDeatilByEmployeeId(talent_id,pointsFormDataId);
                        log.error("detailId:{}",detailId);

                        recordMap.put("detailId",detailId);
                        if(StringUtils.isNotBlank(detailId)){
                            recordMap.put("num","2");
                            employeePointsDao.updatePointsDeatilByFormDataId(talent_id,pointsFormDataId,points);
                            employeePointsDao.updatePointsTotalByFormDataId(pointsFormDataId);

                        }else{
                            recordMap.put("num","3");
                            ebDataChangeResult = ebuilderUtils.bacthAppendEbDetail(talent_id,points,pointsFormDataId);
                            boolean status = false;
                            String message = "";
                            if(ebDataChangeResult != null){
                                status = ebDataChangeResult.getStatus() ;
                                message = ebDataChangeResult.getMessage();
                            }
                            log.error("message:{}",message);
                            if(status){
                                employeePointsDao.updatePointsTotalByFormDataId(pointsFormDataId);
                            }
                            recordMap.put("status",status);
                            recordMap.put("message",message);
                        }
                    }else{
                        recordMap.put("num","4");
                        ebDataChangeResult = ebuilderUtils.bacthInsertDbForm(referrer_id,points,talent_id);
                        boolean status = false;
                        String message = "";
                        if(ebDataChangeResult != null){
                            status = ebDataChangeResult.getStatus() ;
                            message = ebDataChangeResult.getMessage();
                        }
                        log.error("message:{}",message);
                        log.error("status:{}",status);

                        recordMap.put("status",status);
                        recordMap.put("message",message);

                    }
                }else{
                    recordMap.put("message","积分为空");
                }
                recordList.add(recordMap);
            }

        }catch (Exception e){
            Map<String,Object> recordMap = new HashMap<String,Object>();
            recordMap.put("message",e.getMessage());
            recordList.add(recordMap);
        }
        return WeaResult.success(recordList);
    }


    @WeaPermission(publicPermission = true)
    @GetMapping("/test")
    public WeaResult<Object> test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> recordMap = new HashMap<String,Object>();
        recordMap.put("test","test");
        return WeaResult.success(recordMap);
    }


}
