package com.mortals.xhx.module.pj.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.pj.model.PjEvaluateEntity;
import com.mortals.xhx.module.pj.model.PjEvaluateQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.pj.model.PjEvaluateStatEntity;
import com.mortals.xhx.module.pj.service.PjEvaluateStatService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

import com.mortals.xhx.common.code.*;

/**
 * 评价汇总统计
 *
 * @author zxfei
 * @date 2024-06-24
 */
@RestController
@RequestMapping("pj/evaluate/stat")
@Slf4j
public class PjEvaluateStatController extends BaseCRUDJsonBodyMappingController<PjEvaluateStatService, PjEvaluateStatEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private ISiteFeign siteFeign;

    public PjEvaluateStatController() {
        super.setModuleDesc("评价汇总统计");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "pjOption", PjOptionEnum.getEnumMap());
        super.init(model, context);
    }


    @PostMapping(value = "/stat")
    @UnAuth
    public Rest<String> addAttendanceRecordCustom(@RequestBody PjEvaluateQuery pjEvaluateQuery) {
        Rest<String> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询" + this.getModuleDesc();
        int code = 1;
        try {
            //天数区间分段计算
            DateTime attendStart = DateUtil.parseDate(pjEvaluateQuery.getPjTimeStart());
            DateTime attendEnd = DateUtil.parseDate(pjEvaluateQuery.getPjTimeEnd());
            Long compare = DateUtil.between(attendEnd, attendStart, DateUnit.DAY);
            StopWatch stopWatch = new StopWatch("stopwatch");
            log.info("计算天数区间：{}", compare);
            for (int i = 0; i <= compare.intValue(); i++) {
                DateTime curDate = DateUtil.offsetDay(attendStart, i);
                log.info("记录日期：{}", curDate.toDateStr());
                stopWatch.start("执行本地方法");
                SitePdu sitePdu = new SitePdu();
                sitePdu.setSize(-1);
                Rest<RespData<List<SitePdu>>> resp = siteFeign.list(sitePdu);
                if (resp.getCode() == 1) {
                    List<SitePdu> sitePduList = resp.getData().getData();
                    sitePduList.stream().forEach(site -> {
                        PjEvaluateStatEntity sitestatEntity = new PjEvaluateStatEntity();
                        sitestatEntity.initAttrValue();
                        sitestatEntity.setSiteId(site.getId());
                        sitestatEntity.setSiteName(site.getSiteName());
                        sitestatEntity.setSiteCode(site.getSiteCode());

                        sitestatEntity.setYear(curDate.year());
                        sitestatEntity.setMonth(curDate.month() + 1);
                        sitestatEntity.setDay(curDate.dayOfMonth());
                        //设置年月日
                        this.service.updateSitePjStat(sitestatEntity, context);
                    });
                }

                stopWatch.stop();
                log.info("日期：{} 完成,耗时：{}ms", curDate.toDateStr(), stopWatch.getLastTaskTimeMillis());
            }
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = -1;
            this.doException(this.request, busiDesc, model, e);
            model.put("message_info", e.getMessage());
        }

        this.init(model, context);
        ret.setCode(code);
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;

    }


}