package com.younike.admin;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.exception.ServiceException;
import com.younike.system.domain.vo.PublishManageVo;
import com.younike.system.param.PublishQueryParam;
import com.younike.system.service.IPublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发布需求Controller
 * 
 * @author elev
 * @date 2022-01-20
 */
@Api("发布需求接口")
@RestController
@RequestMapping("/admin/publish")
public class AdminPublishController extends BaseController {
    @Autowired
    private IPublishService publishService;

    /**
     * 查询发布需求列表
     */
    @ApiOperation("查询发布需求列表")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping("/list")
    public TableDataInfo list(PublishQueryParam param) {
        startPage();
        List<PublishManageVo> list = publishService.publishAdminList(param);
        return getDataTable(list);
    }

    /**
     * 删除发布需求
     */
    @ApiOperation("删除任务需求")
//    @PreAuthorize("@ss.hasPermi('system:publish:remove')")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @Log(title = "任务需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{publishId}")
    public AjaxResult delete(@PathVariable Long publishId) {
        return publishService.deleteByPublishId(publishId);
    }

    /**
     * 获取发布需求详细信息
     */
    @ApiOperation("获取发布需求详细信息")
    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @GetMapping(value = "/{publishId}")
    public AjaxResult getInfo(@PathVariable("publishId") Long publishId) {
        return AjaxResult.success(publishService.selectPublishByPublishId(publishId));
    }

    @PutMapping("/takeOff/{publishId}")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @ApiOperation("下架需求")
    public AjaxResult takeOff(@PathVariable Long publishId) {

        if(publishId==null){
            throw new ArithmeticException("参数错误");
        }
        return publishService.soldOut(publishId);
    }

    /**
     * 上架
     * @param publishId
     * @return
     */
    @PutMapping("/takeOn/{publishId}")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @ApiOperation("上架需求")
    public AjaxResult takeOn(@PathVariable Long publishId) {

        if(publishId==null){
            throw new ArithmeticException("参数错误");
        }
        return publishService.putaway(publishId);
    }

    /**
     * 查询发布需求详细信息
     */
    @ApiOperation("查询发布需求详细信息")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping("/getDetail/{publishId}")
    public AjaxResult getPublishDetail(@PathVariable("publishId") Long publishId) {
        if (publishId == null) {
            throw new ServiceException("参数publishId不能为空!");
        }
        return publishService.getPublishDetail(publishId);
    }
}
