package com.ruoyi.web.controller.api.csMember;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.CSMemberUserLoginToken;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.CsEvaluate;
import com.ruoyi.cs.domain.CsSetTemplate;
import com.ruoyi.cs.domain.dto.*;
import com.ruoyi.cs.domain.vo.CsResourceOrderApplyVO;
import com.ruoyi.cs.domain.vo.MemberCsResourceOrderApplyVO;
import com.ruoyi.cs.service.ICsEvaluateService;
import com.ruoyi.cs.service.ICsResourceOrderApplyService;
import com.ruoyi.cs.service.ICsSetTemplateService;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: cc
 * @Date: 2024/4/25
 * @Description:用户端——资源申请
 */
@Api("用户端PC/资源申请")
@RestController
@RequestMapping("/api/cs/csResourceOrderApply")
public class ApiCsResourceOrderApplyController extends BaseController {

    @Resource
    private ICsResourceOrderApplyService resourceOrderApplyService;

    @Resource
    private ICsEvaluateService csEvaluateService;

    @Resource
    private ICsSetTemplateService csSetTemplateService;

    /**
     * 新增HPC资源申请
     * @param resourceDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/addHPCResourceApply")
    @Log(title = "新增HPC资源申请", businessType = BusinessType.INSERT)
    public AjaxResult addResourceApply(@RequestBody @Validated({CsResourceOrderApplyADDDTO.addHPCApply.class}) CsResourceOrderApplyADDDTO resourceDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.insertApplyForResource(resourceDTO ));
    }

    /**
     * 新增云计算业务映射资源申请
     * @param resourceDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/addCloudComputingResourceApply")
    @Log(title = "新增云计算业务映射资源申请", businessType = BusinessType.INSERT)
    public AjaxResult addCloudComputingResourceApply(@RequestBody @Validated({CsResourceOrderApplyADDDTO.addCloudComputingApply.class}) CsResourceOrderApplyADDDTO resourceDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.insertApplyForResource(resourceDTO ));
    }

    /**
     * 工单列表
     * @return 结果
     */
    @CSMemberUserLoginToken
    @GetMapping("/getOrderList")
    public AjaxResult getOrderList(MemberResOrderApplyQueryDTO paramDto,
                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<MemberCsResourceOrderApplyVO> result = resourceOrderApplyService.selectOrderApplyListForMember(paramDto);
        return AjaxResult.success( new PageInfo<>(result));
    }

    /**
     * 工单详情
     * @param applyId
     * @return
     */
    @CSMemberUserLoginToken
    @GetMapping("/getResourceOrderDetail")
    public AjaxResult getResourceOrderDetail(String applyId){
        CsResourceOrderApplyVO result = resourceOrderApplyService.applyForResourceDetail(applyId);
        if(result != null){
            if(result.getParallelProgramming() != null){
                if("0".equals(String.valueOf(result.getParallelProgramming()))){
                    result.setParallelProgrammingName("未使用");
                }
                if("1".equals(String.valueOf(result.getParallelProgramming()))){
                    result.setParallelProgrammingName("偶尔使用");
                }
                if("2".equals(String.valueOf(result.getParallelProgramming()))){
                    result.setParallelProgrammingName("经常使用");
                }
            }
            if(result.getUseFrequency() != null){
                if("0".equals(String.valueOf(result.getUseFrequency()))){
                    result.setUseFrequencyName("每日");
                }
                if("1".equals(String.valueOf(result.getUseFrequency()))){
                    result.setUseFrequencyName("每周");
                }
                if("2".equals(String.valueOf(result.getUseFrequency()))){
                    result.setUseFrequencyName("每月");
                }
            }
        }
        return AjaxResult.success(result);
    }

    /**
     *  编辑资源申请基础信息(0待确认/5审核拒绝)
     * @param resourceDTO
     * @param bindingResult
     * @return
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/editResourceApply")
    @Log(title = "编辑资源申请基础信息", businessType = BusinessType.UPDATE)
    public AjaxResult editResourceApply(@RequestBody @Validated CsResourceOrderApplyEditDTOForMember resourceDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.editBaseInfoApplyForMember(resourceDTO));
    }

    /**
     * 确认资源申请详情
     * @param resourceDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/checkResourceApply")
    @Log(title = "确认资源申请详情", businessType = BusinessType.UPDATE)
    public AjaxResult checkResourceApply(@RequestBody @Validated CsResourceOrderApplyEditDTO resourceDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.checkApplyForResource(resourceDTO));
    }

    /**
     * 上传盖章材料
     * @param resourceDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/uploadFileForResource")
    @Log(title = "上传盖章材料", businessType = BusinessType.UPDATE)
    public AjaxResult uploadFileForResource(@RequestBody @Validated(CsResourceOrderApplyEditDTO.uploadFile.class) CsResourceOrderApplyEditDTO resourceDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.uploadFileForResource(resourceDTO));
    }

    /**
     * 服务评价
     * @param evaluateAddDTO 参数
     * @return 结果
     */
    // @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/userServiceEvaluation")
    @Log(title = "服务评价", businessType = BusinessType.INSERT)
    public AjaxResult userServiceEvaluation(@RequestBody CsEvaluateAddDTO evaluateAddDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        CsEvaluate csEvaluate = csEvaluateService.selectCsEvaluateByResId(evaluateAddDTO.getResId());
        if (csEvaluate != null) {
            return AjaxResult.error("已评价，无需重复评价");
        }
        return AjaxResult.success(csEvaluateService.insertCsEvaluateAddDTO(evaluateAddDTO));
    }

    /**
     * 通过表单值获取模版设置详细信息
     */
    @GetMapping("/getInfoByFormValue")
    public AjaxResult getInfoByFormValue(CsSetTemplate csSetTemplate){
        return AjaxResult.success(csSetTemplateService.getInfoByFormValue(csSetTemplate.getFormValue()));
    }



}
