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

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.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.ph.model.PhQueueEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.PjEvaluateEntity;
import com.mortals.xhx.module.pj.service.PjEvaluateService;
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-05-27
 */
@RestController
@RequestMapping("pj/evaluate")
@Slf4j
public class PjEvaluateController extends BaseCRUDJsonBodyMappingController<PjEvaluateService, PjEvaluateEntity, Long> {

    @Autowired
    private ISiteFeign siteFeign;

    public PjEvaluateController() {
        super.setModuleDesc("评价汇总");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "pjOption", PjOptionEnum.getEnumMap());
        this.addDict(model, "pjSource", PjSourceEnum.getEnumMap());
        this.addDict(model, "pjxt", PjxtEnum.getEnumMap());
        this.addDict(model, "evaluatetype", EvaluatetypeEnum.getEnumMap());
        this.addDict(model, "eyevaluate", EyevaluateEnum.getEnumMap());
        this.addDict(model, "pjType", PjTypeEnum.getEnumMap());
        super.init(model, context);
    }

    /**
     * @param entity
     * @return
     */
    @Override
    @UnAuth
    public String save(@RequestBody PjEvaluateEntity entity) {
        //获取站点信息
        getSite(entity);
        return super.save(entity);
    }

    /**
     * @param list
     * @return
     */
    @Override
    @UnAuth
    public String batchSave(@RequestBody List<PjEvaluateEntity> list) {
        for (PjEvaluateEntity entity : list) {
            getSite(entity);
        }
        return super.batchSave(list);
    }


    private void getSite(PjEvaluateEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getSiteId())) {
            Rest<SitePdu> sitePduRest = siteFeign.info(entity.getSiteId());
            if (YesNoEnum.YES.getValue() == sitePduRest.getCode()) {
                SitePdu sitePdu = sitePduRest.getData();

                if (!ObjectUtils.isEmpty(sitePdu)) {
                    entity.setSiteCode(sitePdu.getSiteCode());
                    entity.setSiteName(sitePdu.getSiteName());
                } else {
                    log.info("站点信息不存在,siteId={}", entity.getSiteId());
                }
            }
        }
    }

}