package com.sifang.modules.smallprogram.ItemManagement.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.dao.ItemManageDao;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.AuthUtils;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NeedToDealService extends BaseService {

    @Autowired
    private ItemManageDao itemManageDao;

    //待处理 列表数据
    public List<Record> needToDealList(LayuiPage<Record> page, Map<String, String> map, Operator operator){
        List<String> entryAuth=new ArrayList<>();
        List<String> changeAuth=new ArrayList<>();
        //获取员工的小程序操作权限
        Record r = CrmUtils.getByUserId(operator.getUserType(), operator.getUserId());
        List<String> permission=getEmpSmallAuth(r.getStr("id"));
//        if(permission.contains("HYXCX_SJSH_B")){
//            entryAuth.add(FormUtils.ENTRY_STATUS_SJSH);
//        }
//        if(permission.contains("HYXCX_ANYS_B")){
//            entryAuth.add(FormUtils.ENTRY_STATUS_AZYS);
//        }
//        if(permission.contains("HYXCX_BWYS_B")){
//            entryAuth.add(FormUtils.ENTRY_STATUS_BWYS);
//        }
//        if(permission.contains("HYXCX_DASH_B")){
//            entryAuth.add(FormUtils.ENTRY_STATUS_DASH);
//        }
        entryAuth.add("91");
        entryAuth.add("92");
        entryAuth.add("93");
        entryAuth.add("94");
        List<Record> list= itemManageDao.waiteDealItemList(page,map,operator,entryAuth,changeAuth);
        return list;
    }

    public List<String> getEmpSmallAuth(String empId){
        StringBuffer sql=new StringBuffer();
        sql.append(" SELECT DISTINCT(p.permission) AS permission  ");
        sql.append("  FROM crm_platform_employee_role er, crm_platform_employee_role_rel r, sys_menu_pro p ");
        sql.append(" WHERE r.`employee_id` = ? AND FIND_IN_SET(er.id, r.role_ids) AND p.`parent_id` = '2'  ");
        sql.append("  AND FIND_IN_SET(p.`id`, er.`permission`) AND p.type = '2' ");
        List<Record> records = Db.find(sql.toString(),empId);
        List<String> permissions = records.stream().map(r -> r.getStr("permission")).collect(Collectors.toList());
        return permissions;
    }

}
