package com.sys.bus.jx.service;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sys.bus.model.Cycle;
import com.sys.core.model.PageBean;
import com.sys.core.model.Response;
import com.sys.core.utils.Conditions;
import com.sys.core.utils.Tool;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class JxCycleService {
     /**
     * 保存考核周期
     * @param cycle
     */
    public void addJxCycle(Cycle cycle, Response response){
        String autoName = createAutoName(cycle);
        Cycle  b = getCycleByAutoName(autoName);
        if (b != null && !b.getCycleStatus().equals("2")) {// 2表示无效状态
            response.setCode("2");
            response.setMsg("已经存在相同的考核周期！");
            return;
        }
        Cycle b1 = new Cycle();
        b1.setCycleName(cycle.getCycleName());
        List<Cycle> cycleList = getCycleByWhere(b1);
        if(cycleList!=null && cycleList.size()>0){
            for (Cycle cycle1 : cycleList) {
                if (!cycle1.getCycleStatus().equals("2")) {// 2表示无效状态
                    response.setCode("2");
                    response.setMsg("存在相同名称<"+cycle.getCycleName()+">的考核周期！");
                    return;
                }
            }
        }

        //保存cycle
        cycle.setCycleAutoname(autoName);
        cycle.setCycleId(Tool.uuid());
        cycle.setCycleCreatetime(Tool.getCurrTime());
        //默认为生效状态
        cycle.setCycleStatus("1");
        cycle.save();
    }


    public String createAutoName(Cycle cycle) {
        String type = cycle.getCycleType();
        String year = cycle.getCycleYear();
        if("2".equals(type)){//季度
            String quarter = cycle.getCycleQuarter();
            if (StringUtils.isBlank(year) || StringUtils.isBlank(quarter)) {
                throw new RuntimeException("季度参数异常");
            }
            return year + "年" + quarter + "季度员工考核";

        }else if("3".equals(type)){//年度
            String yeartype = cycle.getCycleYeartype();
            if (StringUtils.isBlank(year) || StringUtils.isBlank(yeartype)) {
                throw new RuntimeException("年度类型参数异常");
            }
            if("1".equals(yeartype)){
                yeartype ="上半年";
            }else if("2".equals(yeartype)){
                yeartype ="下半年";
            }else if("3".equals(yeartype)){
                yeartype ="年度";
            }else{
                throw new RuntimeException("年度参数异常");
            }
            return year + "年" + yeartype + "员工考核";
        }else if("1".equals(type)){//月度
            String month = cycle.getCycleMonth();
            if (StringUtils.isBlank(year) || StringUtils.isBlank(month)) {
                throw new RuntimeException("月度参数异常");
            }
            return year + "年" + month + "月员工考核";

        }
        return null;
    }


    public Cycle getCycleByAutoName(String autoName) {
        Cycle cycle = new Cycle();
        cycle.setCycleAutoname(autoName);
        List<Cycle> list =  getCycleByWhere(cycle);;
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 获取绩效周期分页数据
     * @param cycle
     * @return
     */
    public PageBean getJxCyclePage(Cycle cycle, int pageNumber, int pageSize) {
        if (cycle == null) cycle = new Cycle();
        PageBean pageBean = new PageBean();
        Page<Cycle> page = Cycle.dao.paginate(pageNumber, pageSize, getSqlPara(cycle));
        pageBean.setCount(page.getTotalRow());
        pageBean.setData(page.getList());
        return pageBean;
    }
    /**
     * 获取Cycle的 SqlPara
     *
     * @param cycle
     * @return
     */
    public SqlPara getSqlPara(Cycle cycle) {
        Conditions condi = new Conditions();
        condi.modelToCondition(cycle);
        SqlPara sp = condi.getSqlPara();
        sp.setSql(sp.getSql() + "  order by cycle_createtime desc");
        return sp;
    }




    /**
     * 获取所有状态为有效 的绩效考核周期
     * @return
     */
    public List<Cycle> getJxCycleList(){
        Cycle cycle = new Cycle();
        cycle.setCycleStatus("1");

        return  getCycleByWhere(cycle);
    }

    List<Cycle> getCycleByWhere(Cycle cycle){
        Conditions condi = new Conditions();
        condi.modelToCondition(cycle);
        SqlPara sp = condi.getSqlPara();
        sp.setSql(sp.getSql() + "  order by cycle_createtime desc");
        return Cycle.dao.find(sp);
    }

    /**
     * 删除考核组织
     * @param cycleId
     * @return 0成功   1被引用
     */
    public int deleteJxCycle(String cycleId){
        Cycle cycle = Cycle.dao.findById(cycleId);
        String sql = "select count(*) count from ct_jx_entry where entry_cycleid=? ";
        List<Cycle> cycles = Cycle.dao.find(sql, cycleId);
        int count = cycles.get(0).getInt("count");
        if(count>0){
            return 1;
        }

        cycle.delete();
        return 0;
    }

    /**
     * 根据考核周期ID获取考核周期名称
     */
    public  String getCycleNameById(String cycleId){
        String sql = "select cycle_name from ct_jx_cycle  where  cycle_id = ?";
        List<Cycle> list = Cycle.dao.find(sql,cycleId);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0).getCycleName();
    }
}
