package com.younike.merchant;

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.framework.config.AnonymousAccess;
import com.younike.system.domain.Publish;
import com.younike.system.domain.PublishQuote;
import com.younike.system.domain.PublishQuoteReply;
import com.younike.system.domain.vo.PublishManageVo;
import com.younike.system.domain.vo.PublishQuoteVo;
import com.younike.system.param.PublishQueryParam;
import com.younike.system.service.IPublishQuoteReplyService;
import com.younike.system.service.IPublishQuoteService;
import com.younike.system.service.IPublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
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("/merchant/publish")
public class MerchantPublishController extends BaseController {
    @Autowired
    private IPublishService publishService;
    @Autowired
    private IPublishQuoteReplyService  publishQuoteReplyService;

    @Autowired
    private IPublishQuoteService publishQuoteService;

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

    /**
     * 查询需求报价回复列表
     */
    @ApiOperation("查询需求报价回复列表")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping("/reply/list")
    public TableDataInfo list(PublishQuoteReply publishQuoteReply) {
        startPage();
        List<PublishQuoteReply> list = publishQuoteReplyService.selectPublishQuoteReplyList(publishQuoteReply);
        return getDataTable(list);
    }




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

    /**
     * 修改发布需求
     */
    @ApiOperation("修改发布需求")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @PutMapping
    public AjaxResult edit(@RequestBody Publish publish) {
        return toAjax(publishService.updatePublish(publish));
    }


    /**
     * 新增服务人员报价回复
     */
    @ApiOperation("服务人员报价回复")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @PostMapping("/insertReply")
    public AjaxResult insertReply(@RequestBody PublishQuoteReply publish) {
        if(ObjectUtils.isEmpty(publish) || publish.getQuoteId() == null ||publish.getServerUserId() == null ){
            return AjaxResult.error("参数错误");
        }
        publish.setReplyType("1");
        return publishQuoteReplyService.replyInsert(publish);
    }

    /**
     * 删除报价留言回复信息
     */
    @ApiOperation("删除报价留言回复信息")
    @Log(title = "需求报价", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @DeleteMapping("/{replyId}")
    public AjaxResult remove(@PathVariable Long[] replyId) {
        return toAjax(publishQuoteReplyService.deletePublishQuoteReplyByReplyIds(replyId));
    }

    /**
     * 获取发布需求详细信息(报价集合（包含留言）)
     */
    @ApiOperation("获取发布需求详细信息(报价集合)")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping(value = "/quoteList")
    public TableDataInfo quoteList(PublishQueryParam param) {
        startPage();
        if(ObjectUtils.isEmpty(param) || param.getPublishId()==null){
            throw new ArithmeticException("参数错误");
        }
        List<PublishQuoteVo> list = publishQuoteService.getQuoteList(param.getPublishId());
        return getDataTable(list);
    }


}
