package com.younike.member;

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.MemberAccess;
import com.younike.framework.web.domain.MemberUserContext;
import com.younike.system.domain.PublishQuoteReply;
import com.younike.system.domain.vo.PublishManageVo;
import com.younike.system.domain.vo.PublishQuoteVo;
import com.younike.system.domain.vo.PublishUpdateVo;
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.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    private IPublishQuoteService publishQuoteService;

    @Autowired
    private IPublishQuoteReplyService publishQuoteReplyService;

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

    /**
     * 新增发布需求
     */
    @ApiOperation("新增发布需求")
    @MemberAccess
//    @Log(title = "发布需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insert(@RequestBody PublishUpdateVo publish) {
        return publishService.publishInsert(publish);
    }

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

    /**
     * 获取发布需求详细信息(包含地址信息)
     */
    @ApiOperation("获取发布需求详细信息")
    @MemberAccess
    @GetMapping(value = "/{publishId}")
    public AjaxResult getInfo(@PathVariable("publishId") Long publishId) {
        if(publishId==null){
            throw new ArithmeticException("参数错误");
        }
        return  publishService.getPublishByPublishId(publishId);
    }
    /**
     * 获取发布需求详细信息(报价集合（包含留言）)
     */
    @ApiOperation("获取发布需求详细信息(报价集合)")
    @MemberAccess
    @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("查询我发布需求条数")
    @MemberAccess
    @GetMapping("/mePublishListCount/{memberId}")
    public AjaxResult mePublishListCount(@PathVariable("memberId") Long memberId) {

        if(memberId == null){
            throw new ArithmeticException("参数错误");
        }

        return publishService.mePublishCount(memberId);
    }

    /**
     * 查询我发布需求列表
     */
    @ApiOperation("查询我发布需求列表")
    @MemberAccess
    @GetMapping("/mePublishList")
    public Object mePublishList(PublishQueryParam param) {
        startPage();
        if(ObjectUtils.isEmpty(param) || param.getMemberId()==null){
            throw new ArithmeticException("参数错误");
        }
        List<PublishManageVo> list = publishService.mePublishList(param);
        return getDataTable(list);
    }

    /**
     * 下架
     * @param publishId
     * @return
     */
    @PutMapping("/soldOut/{publishId}")
    @MemberAccess
    @ApiOperation("下架需求")
    public AjaxResult soldOut(@PathVariable Long publishId) {

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

    /**
     * 上架
     * @param publishId
     * @return
     */
    @PutMapping("/putaway/{publishId}")
    @MemberAccess
    @ApiOperation("上架架需求")
    public AjaxResult putaway(@PathVariable Long publishId) {

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

    /**
     * 编辑
     * @param publish
     * @return
     */
    @PutMapping("/editPublish")
    @MemberAccess
    @ApiOperation("编辑需求")
    public AjaxResult editPublish(@RequestBody PublishUpdateVo publish) {

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

    /**
     * 新增报价回复
     */
    @ApiOperation("报价回复")
    @MemberAccess
    @PostMapping("/insertReply")
    public AjaxResult insertReply(@RequestBody PublishQuoteReply publish) {
        if(StringUtils.isBlank(publish.getReplyDesc())){
            return AjaxResult.error("留言不能为空");
        }
        if(ObjectUtils.isEmpty(publish) || publish.getQuoteId() == null ||publish.getMemberId() == null ){
            return AjaxResult.error("参数错误");
        }
        publish.setReplyType("0");
        return publishQuoteReplyService.replyInsert(publish);
    }

    /**
     *  顾客端删除报价回复
     */
    @ApiOperation("删除报价回复")
    @MemberAccess
    @DeleteMapping("delReply/{replyId}")
    public AjaxResult delReply(@PathVariable Long replyId) {
        if(replyId == null ){
            return AjaxResult.error("参数错误");
        }
        return publishQuoteReplyService.delMemberReply(replyId);
    }

}
