package com.sifang.modules.task;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.q7link.openapi.model.ApiParams;
import com.q7link.openapi.model.ApiResponse;
import com.q7link.openapi.model.PostListRequest;
import com.q7link.openapi.model.PostListResult;
import com.sifang.modules.sys.common.utils.IdGen;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static com.sifang.modules.q7link.utils.Q7linkUtils.getSdkRequestConfig;
import static com.sifang.modules.q7link.utils.Q7linkUtils.openapi;

@Component
public class ProjectTask {

    @Scheduled(cron = "0 15 01 * * ?")
    public void testBankQuery() {
        System.out.println("企企同步项目到平台-------");
        PostListRequest request = new PostListRequest();
        ApiParams apiParams = new ApiParams();
        apiParams.setJson("{\n" +
                "  \"objectType\":\"Project\",\n" +
                "  \"fields\": [\n" +
                "        \"code\",\n" +
                "        \"name\",\n" +
                "        \"planEndDate\",\n" +
                "        \"closedDate\",\n" +
                "        \"billStatus\"\n" +

                "  ]\n" +
                "}");
        request.sdkRequestConfig(getSdkRequestConfig(request));
        request.setApiParams(apiParams);
        PostListResult postListResult = openapi().postList(request);
        ApiResponse apiResponse = postListResult.getApiResponse();
        System.out.println("企企同步数据返回结果----"+apiResponse.getJson());
        Map map=(Map) JSONObject.parseObject(apiResponse.getJson());
        String createTime=String.valueOf(DateUtil.date());
        if(map.get("data") != null) {
            Map map1 = (Map) JSONObject.parseObject(String.valueOf(map.get("data")));
            if(map1.get("list") != null) {
                List<Map> list = JSONObject.parseArray(String.valueOf(map1.get("list")), Map.class);
                if(CollectionUtil.isNotEmpty(list)){
                    for(Map m:list){
                        String code=String.valueOf(m.get("code"));
                        String name=String.valueOf(m.get("name"));
                        String planEndDate = "";
                        if (m.get("planEndDate") != null){
                            planEndDate = String.valueOf(m.get("planEndDate"));
                        }
                        String billStatus=String.valueOf(m.get("billStatus"));
                        if ("BillStatus.draft".equals(billStatus)){
                            billStatus = "BillStatus.closed";
                        }
                        Long nowTime=System.currentTimeMillis();
                        String sql="select code,name from crm_supply_material_project where status='0' and code=?  limit 1";
                        List<Record> lists = Db.find(sql,code);
                        if(CollectionUtil.isNotEmpty(lists)){
                            for(Record rd :lists){
                                String nam=rd.get("name");
                                if(!name.equals(nam)){
                                    String updateSql="UPDATE `crm_supply_material_project` SET name = ? WHERE status='0' and code = ?";
                                    Db.update(updateSql, name,code);
                                }

                                if( "BillStatus.closed".equals(billStatus) || nowTime>Long.valueOf(planEndDate)){
                                    String updateSql="UPDATE `crm_supply_material_project` SET status = '1' WHERE  code = ?";
                                    Db.update(updateSql, code);
                                }

                            }
                        }else{
                            if( !"BillStatus.closed".equals(billStatus) && nowTime<Long.valueOf(planEndDate)  ){
                                String insertSql="INSERT INTO `crm_supply_material_project`(`id`, `code`, `name`, `status`, `create_time`) VALUES (?,?,?,'0',?)";
                                Db.update(insertSql, IdGen.uuid(),code,name,createTime);
                            }
                        }
                    }
                }
            }
        }
    }


}
