package com.younike.server.controller;

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.framework.config.ServerAccess;
import com.younike.system.domain.PublishQuoteReply;
import com.younike.system.domain.ServerUser;
import com.younike.system.domain.ServerUserFollow;
import com.younike.system.domain.vo.PublishManageVo;
import com.younike.system.domain.vo.PublishQuoteVo;
import com.younike.system.param.PublishQueryParam;
import com.younike.system.param.PublishQuoteParam;
import com.younike.system.param.ServerUserFollowParam;
import com.younike.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

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

    @Autowired
    private IServerUserFollowService followService;

    @Autowired
    private IPublishQuoteService publishQuoteService;

    @Autowired
    private IPublishQuoteReplyService publishQuoteReplyService;

    @Autowired
    private IServerUserService serverUserService;

    /**
     * 查询发布需求列表
     */
    @ApiOperation("查询发布需求列表")
//    @PreAuthorize("@ss.hasPermi('system:publish:list')")
//    @AnonymousAccess
    @ServerAccess
    @GetMapping("/list")
    public TableDataInfo list(PublishQueryParam param) {

        if(ObjectUtils.isEmpty(param) || param.getServerId() == null){
            throw new ArithmeticException("参数错误");
        }

        /**
         * 根据服务人员id查询数据
         */
        ServerUser serverUser = serverUserService.selectServerUserByServerId(param.getServerId());
        startPage();
        List<PublishManageVo>  list = new ArrayList<>();

        /** 1 关注全部，0 非全部 **/
        if(serverUser.getFollowAll()==0){
            list=  publishService.publishServerList(param);
        }else {
            list = publishService.taskList(param);
        }
        return getDataTable(list);
    }

    /**
     * 查询我报价的需求列表
     */
    @ApiOperation("查询我报价的需求列表")
//    @PreAuthorize("@ss.hasPermi('system:publish:list')")
//    @AnonymousAccess
    @ServerAccess
    @GetMapping("/publishList")
    public TableDataInfo publishList(PublishQueryParam param) {
        startPage();

        if(ObjectUtils.isEmpty(param) || param.getServerId() == null){
            throw new ArithmeticException("参数错误");
        }
        List<PublishManageVo> list = publishService.publishList(param);
        return getDataTable(list);
    }

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

    /**
     * 获取发布需求详细信息包含地址信息)
     */
    @ApiOperation("获取发布需求详细信息")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @GetMapping("/getPublishInfo")
    public AjaxResult getInfo(PublishQueryParam param) {

        if(ObjectUtils.isEmpty(param) || param.getServerId() == null ||param.getPublishId() == null){
            throw new ArithmeticException("参数错误");
        }
        return  publishService.selectPublishBy(param);
     }

    /**
     * 获取发布需求详细信息(报价集合（包含留言）)
     */
    @ApiOperation("获取发布需求详细信息(报价集合)")
    @AnonymousAccess
    @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);
    }

    /**
     * 取消关注服务类型
     */
    @ApiOperation("取消关注服务类型")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @PutMapping("/cancelAttention")
    public AjaxResult cancelAttention(@RequestBody ServerUserFollow follow) {

        if(ObjectUtils.isEmpty(follow) || follow.getCategoryId() == null || follow.getServerUserId() == null){
            return AjaxResult.error("参数错误");
        }
        return followService.cancelAttention(follow);
    }

    /**
     * 提交关注服务类型
     */
    @ApiOperation("提交关注服务类型")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @PutMapping("/attention")
    public AjaxResult attention(@RequestBody ServerUserFollowParam follow) {

        if(ObjectUtils.isEmpty(follow) ||follow.getServerUserId() == null){
            return AjaxResult.error("参数错误");
        }
        return followService.attention(follow);
    }
    /**
     * 新增服务人员报价回复
     */
    @ApiOperation("服务人员报价回复")
    @AnonymousAccess
    @PostMapping("/insertReply")
    public AjaxResult insertReply(@RequestBody PublishQuoteReply publish) {
        if(StringUtils.isBlank(publish.getReplyDesc())){
            return AjaxResult.error("留言不能为空");
        }

        if(ObjectUtils.isEmpty(publish) || publish.getQuoteId() == null ||publish.getServerUserId() == null ){
            return AjaxResult.error("参数错误");
        }
        publish.setReplyType("1");
        return publishQuoteReplyService.replyInsert(publish);
    }
    /**
     * 服务人员删除报价回复
     */
    @ApiOperation("服务人员删除报价回复")
    @AnonymousAccess
    @DeleteMapping("delReply/{replyId}")
    public AjaxResult delReply(@PathVariable Long replyId) {
        if(replyId == null ){
            return AjaxResult.error("参数错误");
        }
        return publishQuoteReplyService.delReply(replyId);
    }

    /**
     * 删除报价
     */
    @ApiOperation("删除报价")
//    @PreAuthorize("@ss.hasPermi('system:publish:remove')")
//    @Log(title = "任务需求", businessType = BusinessType.DELETE)
    @AnonymousAccess
    @DeleteMapping("delQuote/{quoteId}")
    public AjaxResult deleteQuote(@PathVariable Long quoteId) {
        return publishQuoteService.deleteQuote(quoteId);
    }

    /**
     * 提交报价
     */
    @ApiOperation("提交报价")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @PutMapping("/addQuote")
    public AjaxResult addQuote(@RequestBody PublishQuoteParam param) {

        if(ObjectUtils.isEmpty(param) || param.getPublishId()==null || param.getServerUserId() == null ){
            return AjaxResult.error("参数错误");
        }
        if (StringUtils.isBlank(param.getQuoteDesc())) {
            return AjaxResult.error("报价描述不能为空");
        }
        if (param.getQuoteUnit() == null || param.getSinglePrice() == null || param.getQuoteCounts() == null || param.getSingleTime() == null) {
            return AjaxResult.error("报价单位、单价、个数、单个服务时长不可为空");
        }
        return publishQuoteService.AddQuote(param);
    }

    @ApiOperation("是否显示报价按钮")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @GetMapping("/showQuote")
    public AjaxResult showQuote(@RequestParam Long serverId) {

        return publishQuoteService.showQuote(serverId);
    }

    /**
     * 获取订单主详细信息
     */
    @ApiOperation("获取报价详细信息")
    @AnonymousAccess
    @GetMapping(value = "/{quoteId}")
    public AjaxResult getInfo(@PathVariable("quoteId") Long quoteId) {
        return AjaxResult.success(publishQuoteService.selectQuoteByQuoteId(quoteId));
    }

    /**
     * 提交报价
     */
    @ApiOperation("修改报价")
//    @PreAuthorize("@ss.hasPermi('system:publish:query')")
    @AnonymousAccess
    @PutMapping("/editQuote")
    public AjaxResult editQuote(@RequestBody PublishQuoteParam param) {

        if(ObjectUtils.isEmpty(param) || param.getQuoteId()==null ||param.getQuoteUnit() == null || param.getSinglePrice() == null || param.getQuoteCounts() == null){
            return AjaxResult.error("参数错误");
        }
        return publishQuoteService.editQuote(param);
    }

    /**
     * 查询任务广场入口数据
     */
    @ApiOperation("查询任务广场入口数据")
    @ServerAccess
    @GetMapping("/taskList")
    public Object taskList(PublishQueryParam param) {

        if(ObjectUtils.isEmpty(param) || param.getServerId() == null){
            return AjaxResult.error("参数错误");
        }

        /**
         * 根据服务人员id查询数据
         */
        ServerUser serverUser = serverUserService.selectServerUserByServerId(param.getServerId());
        startPage();
        List<PublishManageVo>  list = new ArrayList<>();

        /** 1 关注全部，0 非全部 **/
        if(serverUser.getFollowAll()==0){
            list=  publishService.publishServerList(param);
        }else {
             list = publishService.taskList(param);
        }
        return getDataTable(list);
    }

}
