package com.trs.ggzyexchange.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trs.ggzyexchange.api.vo.Result;
import com.trs.ggzyexchange.entity.*;
import com.trs.ggzyexchange.enums.CommonConstant;
import com.trs.ggzyexchange.service.JgFieldApplyService;
import com.trs.ggzyexchange.service.JgSiteInformationService;
import com.trs.ggzyexchange.utils.RedisUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

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

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author trs
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/jg-site-information")
public class JgSiteInformationController extends BaseController {

    @Autowired
    private JgSiteInformationService jgSiteInformationService;

    @Autowired
    private JgFieldApplyService jgFieldApplyService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping(value = "/list")
    public Result<List<JgSiteInformationVo>> queryPageList(
            @RequestParam(name="sourceCode",defaultValue="2010") String sourceCode,
            @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time
            )
    {
        if(ObjectUtils.isEmpty(time)){
            time = LocalDate.now();
        }
        String redisKey = CommonConstant.JG_SITE_INFORMATION_REDIS;
        if(!ObjectUtils.isEmpty(time)){
            redisKey += time;
        }
        if(!ObjectUtils.isEmpty(sourceCode)){
            redisKey += sourceCode;
        }
        Object JgSiteInformationVoObj = redisUtil.get(redisKey);
        if(!org.springframework.util.ObjectUtils.isEmpty(JgSiteInformationVoObj)){
            try {
                List<JgSiteInformationVo> jgSiteInformationVoList = JSONArray.parseArray(JgSiteInformationVoObj.toString(), JgSiteInformationVo.class);
                if(!ObjectUtils.isEmpty(jgSiteInformationVoList)){
                    return Result.OK(jgSiteInformationVoList);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        String startTime = time.toString()+" 00:00:00";
        String endTime = time.toString()+" 23:59:59";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date dateStart = sdf.parse(startTime); // 将字符串转换为日期
            Date dateEnd = sdf.parse(endTime); // 将字符串转换为日期
            startTime = simpleDateFormat.format(dateStart);
            endTime = simpleDateFormat.format(dateEnd);
        }catch (Exception e){
            e.printStackTrace();
        }
        List<JgSiteInformationVo> jgSiteInformationVoList = new ArrayList<JgSiteInformationVo>();
        QueryWrapper<JgSiteInformation> queryWrapper = new QueryWrapper<JgSiteInformation>();
        queryWrapper.eq("source_data_code",sourceCode);
        queryWrapper.eq("status",1L);
        queryWrapper.orderByAsc("sort");
        List<JgSiteInformation> jgSiteInformationListA = jgSiteInformationService.list(queryWrapper);
        for (JgSiteInformation jgSiteInformation : jgSiteInformationListA) {
            JgSiteInformationVo jgSiteInformationVo = new JgSiteInformationVo();
            jgSiteInformationVo.setIsok(false);
            jgSiteInformationVo.setName(jgSiteInformation.getVenueAddressName());
            try {
                QueryWrapper<JgFieldApply> jgFieldApplyQueryWrapper = new QueryWrapper<JgFieldApply>();
                jgFieldApplyQueryWrapper.gt("BID_OPENING_TIME",startTime);
                jgFieldApplyQueryWrapper.le("BID_OPENING_TIME",endTime);
                try {
                    int count = jgFieldApplyService.count(jgFieldApplyQueryWrapper);
                    Integer pageSize = 100;
                    Integer pageNum = (int)Math.ceil((double)count/pageSize);
                    for(int i = 1; i <= pageNum;i++){
                        Page<JgFieldApply> pageJgFieldApply = new Page<JgFieldApply>(i, pageSize);
                        IPage<JgFieldApply> pageJgFieldApplyList = jgFieldApplyService.page(pageJgFieldApply, jgFieldApplyQueryWrapper);
                        List<JgFieldApply> records = pageJgFieldApplyList.getRecords();
                        if(!ObjectUtils.isEmpty(records)){
                            if(records.size() > 0){
                                for (JgFieldApply record : records) {
                                    String venueCode = record.getVenueCode();
                                    if(!ObjectUtils.isEmpty(venueCode)){
                                        String jgSiteInformationVenueAddressName = jgSiteInformation.getVenueAddressName();
                                        String jgSiteInformationVenueCode = jgSiteInformation.getVenueCode();
                                        if(venueCode.equals(jgSiteInformationVenueAddressName) || venueCode.equals(jgSiteInformationVenueCode)){
                                            jgSiteInformationVo.setIsok(true);
                                        }
                                    }

                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            jgSiteInformationVoList.add(jgSiteInformationVo);
        }
        redisUtil.set(redisKey, JSONObject.toJSONString(jgSiteInformationVoList),CommonConstant.REDIS_SAVE_TIME_TEN_MINUTES);
        return Result.OK(jgSiteInformationVoList);
    }
}
