package net.chenlin.dp.modules.api.service;

import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.StringUtils;
import net.chenlin.dp.common.openapi4j.examples.voucher.VoucherAdd;
import net.chenlin.dp.common.openapi4j.exception.OpenAPIException;
import net.chenlin.dp.common.openapi4j.service.PersonService;
import net.chenlin.dp.common.openapi4j.util.PropUtil;
import net.chenlin.dp.common.utils.JSONUtils;
import net.chenlin.dp.common.utils.SpringContextUtils;
import net.chenlin.dp.modules.api.controller.DingdingOpenInterface;
import net.chenlin.dp.modules.sys.entity.SyViewEntity;
import net.chenlin.dp.modules.sys.service.testU8Service;

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

/**
 * 钉钉备用金
 * 生成至u8总账
 */
public class PettyCashService {

    private testU8Service u8Service= SpringContextUtils.getBean("testU8Service",testU8Service.class);

    public void autoUpdataPetty() throws Exception {

        DingdingOpenInterface dingdingOpenInterface = new DingdingOpenInterface();
        Properties prop = PropUtil.getProperties("/config.properties");
        String PROC = prop.getProperty("byj_process_code");
        List<String> list = dingdingOpenInterface.getOrderIdList(PROC);
//
        List<SyViewEntity> listId = u8Service.selectView("备用金");
        if(list.size() > 0){
            outCycle:for(String o:list){

                if(listId.size() > 0){
                    for(SyViewEntity en:listId){
                        if(!StringUtils.isNullOrEmpty(en.getOaId()) && en.getOaId().equals(o)){
                            continue outCycle;
                        }
                    }
                }

                String job_number = "";//工号
                String work_place = "";//办公地(潍坊对03账套 昆山对01账套)
                String dept_code = ""; //部门编码
                String type = ""; //数据源账套 1昆山,3潍坊
                Map<String,Object> map = dingdingOpenInterface.getOrderDetails(o);
//                Map<String,Object> map = dingdingOpenInterface.getOrderDetails("0861edce-4cfb-4b1d-ac3a-19ed29ae5214");
                if(StringUtils.isNullOrEmpty(map.get("result").toString()) || !map.get("result").toString().equals("agree")){
                    continue;
                }
//                System.out.println(map.get("originator_dept_name"));CHK2019001
                Map<String,Object> map1 = dingdingOpenInterface.tableToMap(map);
                Map<String,Object> mapPerson = dingdingOpenInterface.getViewByUserId(map.get("originator_userid").toString());
//                Map<String,Object> mapPerson = dingdingOpenInterface.getViewByUserId("2006265537678286");
                if(mapPerson !=null ){
                    job_number = mapPerson.get("job_number").toString();
                    work_place = mapPerson.get("work_place").toString();
                    if(StringUtils.isNullOrEmpty(work_place)){
                       continue;
                    }
                    if (work_place.equals("昆山")){
                        dept_code = getPerson(job_number,"1");
                        type = "1";
                    }else if(work_place.equals("潍坊")){
                        dept_code = getPerson(job_number,"3");
                        type = "3";
                    }else{
                        continue;
                    }

                }else{
                    continue;
                }

                map1.put("abstract",map1.get("申请事由"));
                map1.put("naturalCurrency",map1.get("申请金额(元)"));
                map1.put("account_code","122102");//科目编码
                map1.put("dept_code",dept_code);//部门编码
                map1.put("job_number",job_number);//人员编码
                String mess = VoucherAdd.addVoucher(map1,map.get("business_id").toString(),type);

                SyViewEntity entity = new SyViewEntity();
                entity.setOaId(o);
                entity.setAccountCode("122102");
                entity.setParType("备用金");
//                entity.setDeptName(map.get("originator_dept_name").toString());
                entity.setDeptCode(dept_code);
                entity.setJobNumber(job_number);
                entity.setJobNumber("备用金");
                entity.setSyMessage(mess);
                try {
                    u8Service.saveSyView(entity);
                }catch (Exception e){
                    System.out.println("异常备用金");
                }
            }

        }


    }


    /**
     * 根据人员编码查询u8人员信息
     * @param id
     * @param ds
     */
    public static String getPerson(String id,String ds){
        Properties prop = PropUtil.getProperties("/config.properties");
        String to_account = prop.getProperty("to_account");
        PersonService par = new PersonService();
        String code = "";
        try {
            JSONObject record = par.get(id, to_account,ds);

            if(record.get("errcode").equals("0")){
                JSONObject record1 = (JSONObject) record.get("person");
                code = record1.get("cdept_num").toString();
            }
            System.out.println(record);

        } catch (OpenAPIException e) {
            return code;
        }

        return code;
    }


    public static void main(String[] args) throws Exception {
//        autoUpdataPetty();
//        getPerson("CHK2021001",null);
    }


}