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.cs.domain.CsOrderMemberUser;
import com.ruoyi.cs.domain.CsResourceOrderApply;
import com.ruoyi.cs.domain.dto.*;
import com.ruoyi.cs.domain.vo.*;
import com.ruoyi.cs.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
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/csOrderApply")
public class ApiCsOrderApplyController extends BaseController {

    @Resource
    private ICsResourceOrderApplyService resourceOrderApplyService;

    @Resource
    private ICsAchievementInfoDetailsService achievementInfoDetailsService;

    @Resource
    private ICsApprovalRecordService csApprovalRecordService;

    @Resource
    private ICsSetTemplateService csSetTemplateService;

    /**
     * 新增工单申请
     * @param orderDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/addOrderApply")
    @Log(title = "新增工单申请", businessType = BusinessType.INSERT)
    public AjaxResult addOrderApply(@RequestBody @Validated({CsOrderApplyAddDTO.addOrderApply.class}) CsOrderApplyAddDTO orderDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        return AjaxResult.success(resourceOrderApplyService.insertApplyForOrder(orderDTO));
    }

    /**
     * 用户成果信息列表
     * @return 结果
     */
    @CSMemberUserLoginToken
    @GetMapping("/getUserAchievementInfoDetailsList")
    public AjaxResult getUserAchievementInfoDetailsList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<CsAchievementInfoDetailsUserVo> infoDetailsList = achievementInfoDetailsService.selectUserCsAchievementInfoDetailsList();
        return AjaxResult.success( new PageInfo<>(infoDetailsList));
    }

    /**
     * 用户我的工单列表
     * @return 结果
     */
    @CSMemberUserLoginToken
    @GetMapping("/getUserMyOrderList")
    public AjaxResult getUserMyOrderList(MemberMyOrderQueryDTO myOrderQuery,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<MemberMyOrderQueryVo> myOrderQueryVos = resourceOrderApplyService.selectMyOrderApplyListForMember(myOrderQuery);
        return AjaxResult.success( new PageInfo<>(myOrderQueryVos));
    }

    /**
     * 工单详情
     * @param applyId
     * @return
     */
    @CSMemberUserLoginToken
    @GetMapping("/getOrderDetail")
    public AjaxResult getOrderDetail(String applyId){
        CsOrderApplyVO result = resourceOrderApplyService.applyForOrderDetail(applyId);
        return AjaxResult.success(result);
    }

    /**
     * 编辑工单申请
     * @param orderDTO 参数
     * @return 结果
     */
    @CSMemberUserLoginToken
    @RepeatSubmit
    @PostMapping("/editOrderApply")
//    @Log(title = "编辑工单申请", businessType = BusinessType.UPDATE)
    public AjaxResult editOrderApply(@RequestBody CsOrderApplyEditDTO orderDTO, BindingResult bindingResult){
        // 参数校验
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        CsResourceOrderApply resourceOrderApply = resourceOrderApplyService.selectCsResourceOrderApplyByApplyId(orderDTO.getApplyId());
        if (resourceOrderApply == null ) {
            return AjaxResult.error("该状态不可编辑");
        }
        if("5".equals(resourceOrderApply.getApplyStatus())){// 如果审核拒绝的工单编辑后，则状态变为待审核
            orderDTO.setApplyStatus("4");// 4代表待审核
        }
        return AjaxResult.success(resourceOrderApplyService.editApplyForOrder(orderDTO));
    }

    @Autowired
    private ICsOrderMemberUserService csOrderMemberUserService;
    /**
     * 测试
     * @param orderDTO 参数
     * @return 结果
     */
    @GetMapping("/test")
    public AjaxResult test(String resId, String achievementType){
        CsOrderMemberUser csOrderMemberUser = new CsOrderMemberUser();
        csOrderMemberUser.setDelFlag("0");
        List<CsOrderMemberUser> list = csOrderMemberUserService.selectAllUserListByUserType(csOrderMemberUser);
        return AjaxResult.success(list);
    }

    /**
     * 查询最后一条审核记录
     */
    @GetMapping("/selectEndCsApprovalRecordByFormId")
    public AjaxResult selectEndCsApprovalRecordByFormId(String formId)
    {
        return AjaxResult.success(csApprovalRecordService.selectEndCsApprovalRecordByFormId(formId));
    }

    /**
     * 根据模板值查询模板内容
     *
     * @param formValue
     * @return
     */
    @GetMapping("/searchTemplateContentByFormValue")
    public AjaxResult searchTemplateContentByFormValue(String formValue){
        List<CsSetTemplateVO> csSetTemplateVOList = csSetTemplateService.searchTemplateContentByFormValue(formValue);
        return AjaxResult.success(csSetTemplateVOList);
    }

}
