package com.ruoyi.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.code.DictConstant;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.*;
import com.ruoyi.controller.resp.OrderWorksPricePage;
import com.ruoyi.controller.resp.WorksDetailResp;
import com.ruoyi.controller.resp.WorksPageResp;
import com.ruoyi.frequency.worklike.service.WorkLikeService;
import com.ruoyi.frequency.works.entity.Works;
import com.ruoyi.frequency.works.service.WorksService;
import com.ruoyi.frequency.workstep.service.WorkStepService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.BodyIdReq;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作品
 */
@CrossOrigin
@RestController
@RequestMapping("/api/works")
@Api(tags = "作品")
public class ApiWorksController extends RyController {


    @Autowired
    private WorksService worksService;

    @Autowired
    private BusinessUtil businessUtil;

    @PostMapping("/worksRelease")
    @ApiOperation(value = "作品发布", notes = "作品发布")
    @UserLoginToken
    public ResponseData worksRelease(@RequestBody WorksReleaseReq req) {


        UserVo userVo = getUserVo();

        //最大上传作品数量
        Integer bigNum = Integer.valueOf(businessUtil.getDict(DictConstant.作品最大数量).getDictValue());

        //判断作品上传数量
        int worksNum = worksService.count(new QueryWrapper<Works>().lambda()
                .isNull(Works::getOrderId)
                .ne(Works::getDelFlag, PublicCommon.删除)
                .eq(Works::getStoreId, userVo.getUserId()));

        if (worksNum >= bigNum) {
            return ResponseData.error("作品上传数量不能超过" + bigNum + "个");
        }

        return worksService.worksRelease(req, userVo);
    }


    @PostMapping("/worksPage")
    @ApiOperation(value = "作品列表", notes = "作品列表")
    public ResponseData<PageResult<Page<WorksPageResp>>> worksPage(@RequestBody WorksPageReq req) {
        req.setShowEnterprise(2);
        return worksService.worksPage(req, getUserVo());
    }

    @PostMapping("/worksPageBySelf")
    @ApiOperation(value = "我的作品(自己查看)", notes = "我的作品(自己查看)")
    public ResponseData<PageResult<Page<WorksPageResp>>> worksPageBySelf(@RequestBody WorksPageReq req) {
        UserVo userVo = getUserVo();
        req.setUserId(userVo.getUserId());
        req.setUserType(userVo.getUserType());
        req.setIsSelf(1);
        return worksService.worksPage(req, userVo);
    }

    @PostMapping("/worksPageByOtherSelf")
    @ApiOperation(value = "我的作品(他人查看)", notes = "我的作品(他人查看)")
    public ResponseData<PageResult<Page<WorksPageResp>>> worksPageByOtherSelf(@RequestBody WorksPageReq req) {
        return worksService.worksPage(req, getUserVo());
    }


    @PostMapping("/worksDetail")
    @ApiOperation(value = "作品详情", notes = "作品详情")
    public ResponseData<WorksDetailResp> worksDetail(@RequestBody BodyIdReq req) {
        return worksService.worksDetail(req.getId(), req.getIsAddBrowseNum(), getUserVo());
    }

    @Autowired
    private WorkLikeService workLikeService;

    @PostMapping("/worksLike")
    @UserLoginToken
    @ApiOperation(value = "作品点赞", notes = "作品点赞")
    public ResponseData worksLike(@RequestBody WorksLikeReq req) {
        return workLikeService.worksLike(req, getUserVo());
    }

    @PostMapping("/hideWork")
    @UserLoginToken
    @ApiOperation(value = "隐藏作品", notes = "隐藏作品")
    public ResponseData hideWork(@RequestBody BodyIdReq req) {
        return worksService.hideWork(req, getUserVo());
    }


    @Autowired
    private WorkStepService workStepService;

    @PostMapping("/worksStep")
    @UserLoginToken
    @ApiOperation(value = "作品踩赞", notes = "作品踩赞")
    public ResponseData worksStep(@RequestBody WorksStepReq req) {
        return workStepService.worksStep(req, getUserVo());
    }


    @PostMapping("/drawingDetailsPage")
    @ApiOperation(value = "制图明细", notes = "制图明细")
    public ResponseData<OrderWorksPricePage> drawingDetailsPage(@RequestBody DrawingDetailsPageReq req) {
        return worksService.drawingDetailsPage(req, getUserVo());
    }


    /**
     * 获取有作品的分类
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHaveWorkCategory", method = RequestMethod.POST)
    @ApiOperation(value = "字典表下级")
    public ResponseData<List<SysDictData>> getHaveWorkCategory(@RequestBody WorksPageReq req) {
        return worksService.getHaveWorkCategory(req);
    }
}
