package com.ho.business.controller;

import com.ho.business.entity.PlanningCurveTactics;
import com.ho.business.feignclient.UserFeignClient;
import com.ho.business.service.PlanningCurveService;
import com.ho.business.service.PlanningCurveTacticsService;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.UserDetailRespVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.user.api.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author catkins
 * @date 2023/10/12 0012 17:33
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "planningTactics")
@Api(tags = "计划曲线策略")
@Slf4j
public class PlanningCurveTacticsController {
    @Autowired
    private PlanningCurveTacticsService planningCurveTacticsService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserFeignClient userFeignClient;


    @PostMapping("list")
    @ApiOperation(value = "策略列表查询")
    @TokenIgnore
    public DataResult getTacticsList(@RequestBody PlanningCurveTactics tactics) {
        if( tactics != null && tactics.getPageNum() != null ){
            tactics.setPageNum(tactics.getPageNum()-1);
        }
        List<PlanningCurveTactics> tacticsList = planningCurveTacticsService.getTacticsList(tactics);
        tacticsList.stream().forEach( t -> {
            Integer type = t.getType();
            switch(type){
                case 1:
                    t.setTypeName("节假日");
                    break;
                case 2:
                    t.setTypeName("周末");
                    break;
                default:
                    t.setTypeName("日常");
                    break;
            }
        } );
        return DataResult.success(tacticsList);
    }

    @PostMapping("details")
    @ApiOperation(value = "策略查看详情")
    @TokenIgnore
    public DataResult getTacticsDetails(@RequestBody PlanningCurveTactics tactics) {
        PlanningCurveTactics tacticsDetails = planningCurveTacticsService.getTacticsDetails(tactics);
        return DataResult.success(tacticsDetails);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    @TokenIgnore
    public DataResult add(HttpServletRequest request,@RequestBody PlanningCurveTactics tactics) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        SysUser user = null;
        if( simpleUser != null ){
            DataResult<SysUser> userById = userFeignClient.getUserById(simpleUser.getUserId());
            user = userById.getData();
        } else {
            throw new BusinessException(BaseResponseCode.TOKEN_PARSE_ERROR);
        }
        if( tactics != null && user != null ){
            tactics.setCreator(user.getUsername());
        }
        planningCurveTacticsService.add(tactics);
        return DataResult.success();
    }

    @PostMapping("modify")
    @ApiOperation(value = "修改")
    @TokenIgnore
    public DataResult modify(HttpServletRequest request,@RequestBody PlanningCurveTactics tactics) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        SysUser user = null;
        if( simpleUser != null ){
            DataResult<SysUser> userById = userFeignClient.getUserById(simpleUser.getUserId());
            user = userById.getData();
            //user = userFeignClient.getUserById(simpleUser.getUserId());
        }
        if( tactics != null ){
            tactics.setModifier(user.getUsername());
        }
        planningCurveTacticsService.modify(tactics);
        return DataResult.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    @TokenIgnore
    public DataResult delete(@RequestBody PlanningCurveTactics tactics) {
        planningCurveTacticsService.delete(tactics);
        return DataResult.success();
    }

    @PostMapping("getPlanningCurveTemplates")
    @ApiOperation(value = "查询下拉框数据")
    @TokenIgnore
    public DataResult getPlanningCurveTemplates(@RequestBody PlanningCurveTactics tactics){
        List<PlanningCurveTactics> tacticsList = planningCurveTacticsService.getPlanningCurveTemplates(tactics);
        return DataResult.success(tacticsList);
    }

}
