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

import com.alibaba.fastjson.JSON;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
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.pj.model.PjEvaluateEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
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.ph.model.PhQueueEntity;
import com.mortals.xhx.module.ph.service.PhQueueService;
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("ph/queue")
@Slf4j
public class PhQueueController extends BaseCRUDJsonBodyMappingController<PhQueueService, PhQueueEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private ISiteFeign siteFeign;

    public PhQueueController() {
        super.setModuleDesc("排号汇总");
    }

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


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

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


    private void getSite(PhQueueEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getSiteId())) {

            RedisTemplate<String, String> baseRedisTemplate = cacheService.selectDbRedisTemplate(1);

            //com:mortals:xhx:module:site:model:SiteEntity  redis调用
            String siteStr = (String) baseRedisTemplate.opsForHash().get("com:mortals:xhx:module:site:model:SiteEntity", entity.getSiteId());
            if (!ObjectUtils.isEmpty(siteStr)) {
                SitePdu sitePdu = JSON.parseObject(siteStr, SitePdu.class);
                if (!ObjectUtils.isEmpty(sitePdu)) {
                    entity.setSiteCode(sitePdu.getSiteCode());
                    entity.setSiteName(sitePdu.getSiteName());
                } else {
                    log.info("站点信息不存在,siteId={}", entity.getSiteId());
                }
            }

            //feign调用
         /*   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());
                }
            }*/
        }
    }


}