package com.mortals.xhx.module.access.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.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.util.ThreadPool;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.common.code.StatTypeEnum;
import com.mortals.xhx.common.code.TypeEnum;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.thread.StatThread;
import com.mortals.xhx.module.access.model.AccessEntity;
import com.mortals.xhx.module.access.model.AccessQuery;
import com.mortals.xhx.module.access.service.AccessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 区域接入
 *
 * @author zxfei
 * @date 2024-07-02
 */
@RestController
@RequestMapping("access")
@Slf4j
public class AccessController extends BaseCRUDJsonBodyMappingController<AccessService, AccessEntity, Long> {
    @Autowired
    private AccessService accessService;

    public AccessController() {
        super.setModuleDesc("区域接入");
    }

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

    @Override
    protected void doListBefore(AccessEntity query, Map<String, Object> model, Context context) throws AppException {
        query.setOrderColList(Arrays.asList(new OrderCol("sort")));

        super.doListBefore(query, model, context);
    }

    @PostMapping(value = "/stat")
    @UnAuth
    public Rest<String> stat(@RequestBody AccessQuery accessQuery) {
        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(accessQuery.getAccessTimeStart());
            DateTime attendEnd = DateUtil.parseDate(accessQuery.getAccessTimeEnd());
            Long compare = DateUtil.between(attendEnd, attendStart, DateUnit.DAY);
            log.info("计算天数区间：{}", compare);
            accessQuery.setAccessTimeStart(null);
            accessQuery.setAccessTimeEnd(null);

            if (ObjectUtils.isEmpty(accessQuery.getTypeStatusList())) {
                accessQuery.setTypeStatusList(Arrays.asList(StatTypeEnum.STAT_PH.getValue(), StatTypeEnum.STAT_PJ.getValue()));
            }

            List<SitePdu> statSiteList = accessService.getStatSiteList(accessQuery);
            for (SitePdu site : statSiteList) {
                StatThread statThread = new StatThread(attendStart, compare, site, context, accessQuery.getTypeStatusList());
                ThreadPool.getInstance().execute(statThread);
            }
            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;
    }
}