package com.engine.mingfeng.service.impl;

import com.engine.core.impl.Service;
import com.engine.mingfeng.service.PerformanService;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

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

/**
 * @author chenwnj
 * @date 2023/11/6
 * @description
 **/
public class PerformanServiceImpl extends Service implements PerformanService {
    @Override
    public Map<String, Object> getPerformanInfo(Map<String, Object> params) {

        BaseBean bb = new BaseBean();
        bb.writeLog("-----getPerformanInfo start-----");

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        RecordSet rs = new RecordSet();
        try {
            User user = getUser();

            int userId = user.getUID();
            bb.writeLog("userId: " + userId);

            List<Map<String, Object>> performanInfos = new ArrayList<>();
            if (userId>0) {
                String acqPerformanSql = "select case lb when 0 then '业绩达成-材料' " +
                        "                                when 1 then '业绩达成-设备' " +
                        "                                when 2 then 'O2O' " +
                        "                                when 3 then 'GI 非标 - 个人' " +
                        "                                when 4 then '成本改善' END as lb , ndmb, ytddc, ytddcbl, ycpd from uf_jxzssj where xm = ?";
                bb.writeLog("acqPerformanSql: " + acqPerformanSql);

                rs.executeQuery(acqPerformanSql, userId);
                while (rs.next()) {
                    String lb = Util.null2String(rs.getString("lb"));//年度目标
                    String ndmb = Util.null2String(rs.getString("ndmb"));//年度目标
                    String ytddc = Util.null2String(rs.getString("ytddc"));//YTD达成
                    String ytddcbl = Util.null2String(rs.getString("ytddcbl"));//YTD达成比例
                    String ycpd = Util.null2String(rs.getString("ycpd"));//异常情况

                    bb.writeLog("lb: " + lb);
                    bb.writeLog("ndmb: " + ndmb);
                    bb.writeLog("ytddc: " + ytddc);
                    bb.writeLog("ytddcbl: " + ytddcbl);
                    bb.writeLog("ycpd: " + ycpd);

                    if ( StringUtils.isNotBlank(ytddcbl)) {
                        int index = ytddcbl.indexOf("%");
                        if (index >= 0) {
                            ytddcbl = ytddcbl.substring(0, index );
                        }
                    }
                    bb.writeLog("ytddcbl: " + ytddcbl);

                    String wdc = "0";
                    if ( StringUtils.isNotBlank(ndmb) && StringUtils.isNotBlank(ytddc)) {
                        wdc = String.valueOf(Double.parseDouble(ndmb)-Double.parseDouble(ytddc));
                    }
                    bb.writeLog("wdc: " + wdc);

                    data = new HashMap<>();
                    data.put("lb",lb);
                    data.put("ndmb",ndmb);
                    data.put("ytddc",ytddc);
                    data.put("ytddcbl",ytddcbl);
                    data.put("wdc",wdc);
                    data.put("ycpd",ycpd);
                    performanInfos.add(data);
                }
            }
            result.put("code",200);
            result.put("msg","请求成功");
            result.put("data", performanInfos);
        } catch (Exception e) {
            bb.writeLog("getPerformanInfo exception: " + e);
            result.put("code",500);
            result.put("msg","请求异常，请联系管理员");
        }

        bb.writeLog("-----getPerformanInfo end-----");

        return result;
    }
}
