package com.ylx.web.controller.massage;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.common.core.domain.model.LoginUser;
import com.ylx.massage.domain.MaProject;
import com.ylx.massage.domain.dto.MaProjectAuditQueryDTO;
import com.ylx.massage.domain.dto.MaProjectAuditSubmitDTO;
import com.ylx.massage.domain.dto.MaProjectUpdateEnableDTO;
import com.ylx.massage.domain.vo.MaProjectAuditListVO;
import com.ylx.massage.domain.vo.MaProjectListVo;
import com.ylx.massage.domain.vo.MaProjectOpenServiceVO;
import com.ylx.massage.domain.vo.MaProjectSaveVo;
import com.ylx.massage.service.IMaProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.AjaxResult;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.core.page.TableDataInfo;

/**
 * 服务项目Controller
 *
 * @author ylx
 * @date 2024-03-20
 */
@Api("服务项目管理")
@RestController
@RequestMapping("/massage/project")
public class MaProjectController extends BaseController {
    @Autowired
    private IMaProjectService maProjectService;

    /**
     * 查询服务项目列表
     */
    @ApiOperation("PC查询服务项目列表")
    @PreAuthorize("@ss.hasPermi('massage:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaProject maProject) {
        startPage();
        List<MaProjectListVo> list = maProjectService.selectMaProjectList(maProject);
        return getDataTable(list);
    }

    /**
     * 查询技能审核列表
     *
     * @param page 分页参数
     * @param dto  查询参数
     * @return R<Page<MaProjectAuditListVO>> 分页结果
     */
    @ApiOperation("后台查询技能审核列表")
    @PreAuthorize("@ss.hasPermi('massage:project:list')")
    @GetMapping("/audit/list")
    public R<Page<MaProjectAuditListVO>> auditList(Page<MaProjectAuditListVO> page, MaProjectAuditQueryDTO dto) {
        try {
            return R.ok(maProjectService.selectProjectAuditList(page, dto));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询商户申请开通的项目列表
     *
     * @param merchantId 商户ID
     * @return R<List<MaProjectOpenServiceVO>> 商户申请开通项目列表
     */
    @ApiOperation("后台查询商户申请开通的项目列表")
    @PreAuthorize("@ss.hasPermi('massage:project:list')")
    @GetMapping("/audit/{merchantId}/projects")
    public R<List<MaProjectOpenServiceVO>> auditProjectList(@PathVariable("merchantId") Long merchantId) {
        try {
            return R.ok(maProjectService.selectMerchantOpenProjectList(merchantId));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 审核商户申请开通的项目
     *
     * @param projectAuditId 技能审核记录ID
     * @param dto            审核提交参数
     * @return AjaxResult 审核结果
     */
    @ApiOperation("后台审核商户申请开通的项目")
    @PreAuthorize("@ss.hasPermi('massage:project:edit')")
    @Log(title = "技能开通审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/project/{projectAuditId}/submit")
    public AjaxResult submitProjectAudit(@PathVariable("projectAuditId") Long projectAuditId, @RequestBody MaProjectAuditSubmitDTO dto) {
        try {
            LoginUser loginUser = getLoginUser();
            return toAjax(maProjectService.submitProjectAudit(projectAuditId, dto, loginUser));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 是否上下架商户申请开通的项目
     *
     * @param dto 上下架参数
     * @return AjaxResult 上下架结果
     */
    @ApiOperation("是否上下架商户申请开通的项目")
    @PutMapping("/audit/project/enable")
    public R updateProjectEnable(@RequestBody MaProjectUpdateEnableDTO dto) {
        try {
            LoginUser loginUser = getLoginUser();
            return R.ok(maProjectService.updateProjectEnable(dto));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 获取服务项目详细信息
     */
    @ApiOperation("PC获取服务项目详细信息")
    @PreAuthorize("@ss.hasPermi('massage:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            return success(maProjectService.selectMaProjectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增服务项目
     */
    @ApiOperation("新增/修改服务项目")
    @PreAuthorize("@ss.hasPermi('massage:project:add')")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public AjaxResult add(@RequestBody MaProjectSaveVo maProject) {
        LoginUser loginUser = getLoginUser();
        return toAjax(maProjectService.insertMaProject(maProject, loginUser));
    }


    /**
     * 删除服务项目
     */
    @ApiOperation("删除服务项目")
    @PreAuthorize("@ss.hasPermi('massage:project:remove')")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(maProjectService.deleteMaProjectById(id));
    }
}
