package com.trs.ggzyexchange.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.service.*;
import com.trs.ggzyexchange.utils.RestHighLevelClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 20.6 社会信用信息 前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-03-03
 */
@Slf4j
@RestController
@RequestMapping("/xy-social-credit")
public class XySocialCreditController extends BaseController {

    @Autowired
    private XySocialCreditService xySocialCreditService;

    @Autowired
    private XyBlacklistService xyBlacklistService;

    @Autowired
    private XyViolatinService xyViolatinService;

    @Autowired
    private XyRewardService xyRewardService;

    @Autowired
    private XyPerformanceService xyPerformanceService;

    @Autowired
    private RestHighLevelClientUtils restHighLevelClientUtils;

    /**
     * 信用信息
     * @return
     * @throws JsonProcessingException
     */
    //@GetMapping(value = "/queryPageListUpdateEs1")
    public Integer queryPageListUpdateEs1() throws JsonProcessingException {
        int count = xySocialCreditService.count();
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<XySocialCreditVo> xySocialCreditVos = new ArrayList<XySocialCreditVo>();

        for(int i = 1; i <= pageNum;i++){
            Page<XySocialCredit> page = new Page<XySocialCredit>(pageNum, pageSize);
            IPage<XySocialCredit> recordsList = xySocialCreditService.page(page);
            List<XySocialCredit> records = recordsList.getRecords();

            for (XySocialCredit record : records) {
                String id = record.getSourceDataKey();//主键
                String code = record.getBodyCode();//主体代码
                String name = record.getBodyName();//主体名称
                String area = record.getArea();//行政区域
                XySocialCreditVo xySocialCreditVo = new XySocialCreditVo();
                xySocialCreditVo.setId(id);
                xySocialCreditVo.setCode(code);
                xySocialCreditVo.setName(name);
                xySocialCreditVo.setArea(area);
                xySocialCreditVo.setCreditType("信用信息");
                xySocialCreditVos.add(xySocialCreditVo);
            }
            parseXySocialCreditVo(xySocialCreditVos);
        }
        return xySocialCreditVos.size();
    }

    /**
     * 违法违规信息
     * @return
     * @throws JsonProcessingException
     */
    //@GetMapping(value = "/queryPageListUpdateEs2")
    public Integer queryPageListUpdateEs2() throws JsonProcessingException {
        int count = xyViolatinService.count();
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<XySocialCreditVo> xySocialCreditVos = new ArrayList<XySocialCreditVo>();

        for(int i = 1; i <= pageNum;i++){
            Page<XyViolatin> page = new Page<XyViolatin>(pageNum, pageSize);
            IPage<XyViolatin> recordsList = xyViolatinService.page(page);
            List<XyViolatin> records = recordsList.getRecords();

            for (XyViolatin record : records) {
                String id = record.getSourceDataKey();//主键
                String code = record.getBodyCode();//主体代码
                String name = record.getBodyName();//主体名称
                String punishType = record.getViolatingType();//违法违规类型
                String behavior = record.getConfinedAction();//限制的行为
                XySocialCreditVo xySocialCreditVo = new XySocialCreditVo();
                xySocialCreditVo.setId(id);
                xySocialCreditVo.setCode(code);
                xySocialCreditVo.setName(name);
                xySocialCreditVo.setPunishType(punishType);
                xySocialCreditVo.setBehavior(behavior);
                xySocialCreditVo.setCreditType("违法违规信息");
                xySocialCreditVos.add(xySocialCreditVo);
            }
            parseXySocialCreditVo(xySocialCreditVos);
        }
        return xySocialCreditVos.size();
    }

    /**
     * 黑名单
     * @return
     * @throws JsonProcessingException
     */
    //@GetMapping(value = "/queryPageListUpdateEs3")
    public Integer queryPageListUpdateEs3() throws JsonProcessingException {
        int count = xyBlacklistService.count();
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<XySocialCreditVo> xySocialCreditVos = new ArrayList<XySocialCreditVo>();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String format1 = "";

        for(int i = 1; i <= pageNum;i++){
            Page<XyBlacklist> page = new Page<XyBlacklist>(pageNum, pageSize);
            IPage<XyBlacklist> recordsList = xyBlacklistService.page(page);
            List<XyBlacklist> records = recordsList.getRecords();

            for (XyBlacklist record : records) {
                String id = record.getSourceDataKey();//主键
                String code = record.getBodyCode();//主体代码
                String name = record.getBodyName();//主体名称
                Long pubTime = record.getRecordDate();//发布时间

                try {
                    date = format.parse(pubTime+"");
                    format1 = format2.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                String behavior = record.getConfinedAction();//限制的行为

                XySocialCreditVo xySocialCreditVo = new XySocialCreditVo();
                xySocialCreditVo.setId(id);
                xySocialCreditVo.setCode(code);
                xySocialCreditVo.setName(name);
                xySocialCreditVo.setPubTime(format1);
                xySocialCreditVo.setBehavior(behavior);
                xySocialCreditVo.setCreditType("黑名单信息");
                xySocialCreditVos.add(xySocialCreditVo);
            }
            parseXySocialCreditVo(xySocialCreditVos);
        }
        return xySocialCreditVos.size();
    }

    /**
     * 奖励
     * @return
     * @throws JsonProcessingException
     */
    //@GetMapping(value = "/queryPageListUpdateEs4")
    public Integer queryPageListUpdateEs4() throws JsonProcessingException {
        int count = xyRewardService.count();
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<XySocialCreditVo> xySocialCreditVos = new ArrayList<XySocialCreditVo>();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String format1 = "";

        for(int i = 1; i <= pageNum;i++){
            Page<XyReward> page = new Page<XyReward>(pageNum, pageSize);
            IPage<XyReward> recordsList = xyRewardService.page(page);
            List<XyReward> records = recordsList.getRecords();

            for (XyReward record : records) {
                String id = record.getSourceDataKey();//主键
                String code = record.getBodyCode();//主体代码
                String name = record.getBodyName();//主体名称
                Long pubTime = record.getRewardDate();//发布时间

                try {
                    date = format.parse(pubTime+"");
                    format1 = format2.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                String punishType = record.getRewardPunishmentType();

                XySocialCreditVo xySocialCreditVo = new XySocialCreditVo();
                xySocialCreditVo.setId(id);
                xySocialCreditVo.setCode(code);
                xySocialCreditVo.setName(name);
                xySocialCreditVo.setPubTime(format1);
                xySocialCreditVo.setPunishType(punishType);
                xySocialCreditVo.setCreditType("奖励信息");
                xySocialCreditVos.add(xySocialCreditVo);
            }
            parseXySocialCreditVo(xySocialCreditVos);
        }
        return xySocialCreditVos.size();
    }

    /**
     * 履约
     * @return
     * @throws JsonProcessingException
     */
    //@GetMapping(value = "/queryPageListUpdateEs5")
    public Integer queryPageListUpdateEs5() throws JsonProcessingException {
        int count = xyPerformanceService.count();
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Integer pageNum = (int)Math.ceil((double)count/pageSize);
        List<XySocialCreditVo> xySocialCreditVos = new ArrayList<XySocialCreditVo>();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String format1 = "";

        for(int i = 1; i <= pageNum;i++){
            Page<XyPerformance> page = new Page<XyPerformance>(pageNum, pageSize);
            IPage<XyPerformance> recordsList = xyPerformanceService.page(page);
            List<XyPerformance> records = recordsList.getRecords();

            for (XyPerformance record : records) {
                String id = record.getSourceDataKey();//主键
                String code = record.getBodyCode();//主体代码
                String name = record.getBodyName();//主体名称
                String projectName = record.getTenderProjectName();//行政区域

                Long pubTime = record.getPerformanceSubmittedTime();//发布时间
                try {
                    date = format.parse(pubTime+"");
                    format1 = format2.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                XySocialCreditVo xySocialCreditVo = new XySocialCreditVo();
                xySocialCreditVo.setId(id);
                xySocialCreditVo.setCode(code);
                xySocialCreditVo.setName(name);
                xySocialCreditVo.setProjectName(projectName);
                xySocialCreditVo.setPubTime(format1);
                xySocialCreditVo.setCreditType("履约信息");
                xySocialCreditVos.add(xySocialCreditVo);
            }
            parseXySocialCreditVo(xySocialCreditVos);
        }
        return xySocialCreditVos.size();
    }

    public Boolean  parseXySocialCreditVo(List<XySocialCreditVo> xySocialCreditVos) throws JsonProcessingException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
        ObjectMapper objectMapper = new ObjectMapper();
        IndexResponse info = null;
        try{
            for (XySocialCreditVo xySocialCreditVo : xySocialCreditVos) {
                String toJSONString = objectMapper.writeValueAsString(xySocialCreditVo);
                log.info("同步数据-主键："+xySocialCreditVo.getId());
                log.info("同步数据-主体名称："+xySocialCreditVo.getName());
                info = restHighLevelClientUtils.addDoc("public_resource_deal_company", String.valueOf(xySocialCreditVo.getId()), toJSONString);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            flag =  info.status().toString().equals("OK");
            flag = true;
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }

}
