package com.mcwl.web.controller.resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.mcwl.common.core.controller.BaseController;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.ModelProduct;
import com.mcwl.resource.domain.ModelVersion;
import com.mcwl.resource.domain.dto.ModelImagePageRes;
import com.mcwl.resource.domain.response.ResponseModelProduct;
import com.mcwl.resource.domain.request.RequestModel;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.ModelVersionMapper;
import com.mcwl.resource.service.*;
import com.mcwl.system.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 模型
 *
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.web.controller.resource
 * @Filename：MallProductController
 * @Description 商品
 * @Date：2024/12/31 10:48
 */
@Api(tags = "模型")
@RestController
@RequestMapping("/model")
public class MallProductController extends BaseController {


    @Autowired
    private ModelService modelService;

    @Autowired
    private ModelVersionMapper modelVersionMapper;

    @Autowired
    private SysUserServiceImpl sysUserService;


    /**
     * 模型列表
     */
    @ApiOperation(value = "模型列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ModelImagePageRes imagePageRes) {

        return modelService.listByPage(imagePageRes);
    }


    @ApiOperation(value = "模型详情")
    @GetMapping("finbyid")
    public AjaxResult finbyid(@Valid @NotNull(message = "模型id不能为空") @RequestParam Long id) {

        RequestModel requestModel = new RequestModel();

        ModelProduct modelVersion1 = modelService.getById(id);

        LambdaQueryWrapper<ModelVersion> modelVersionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getModelId,id);
        modelVersionLambdaQueryWrapper.eq(ModelVersion::getDelFlag,"0");
        List<ModelVersion> modelVersions = modelVersionMapper.selectList(modelVersionLambdaQueryWrapper);

        requestModel.setModelProduct(modelVersion1);
        requestModel.setModelVersionList(modelVersions);
        return AjaxResult.success(requestModel);
    }


    @ApiOperation(value = "添加模型")
    @PostMapping("/insert")
    public R<String> addupdateModel(@RequestBody RequestModel requestModel) {
        ModelProduct modelProduct = requestModel.getModelProduct();
        Long userId = SecurityUtils.getUserId();
        modelProduct.setUserId(userId);
        modelProduct.setCreateBy(getUsername());
        return modelService.addModel(requestModel);
    }

    @ApiOperation(value = "修改模型")
    @PostMapping("/update")
    public AjaxResult updateModel(@RequestBody RequestModel requestModel) {
        ModelProduct modelProduct = requestModel.getModelProduct();

        Long userId = SecurityUtils.getUserId();
        modelProduct.setUserId(userId);
        modelProduct.setUpdateBy(getUsername());
        modelService.updaModel(requestModel);

        return AjaxResult.success("修改成功");
    }


    @ApiOperation(value = "删除模型")
    @GetMapping("delete")
    public AjaxResult delete(@Valid @NotNull(message = "模型id不能为空") @RequestParam Long id) {
        modelService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 查询模型详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询模型详情")
    @GetMapping("/selectModelById")
    public R<ModelProduct> selectModelById(@Valid @NotNull(message = "模型id不能为空") @RequestParam Long id) {

        return modelService.selectModelById(id);
    }


    /**
     * 设置模型置顶状态
     *
     * @param id
     * @param isTop
     * @return
     */
    @ApiOperation(value = "设置模型置顶状态")
    @GetMapping("/{id}/top")
    public AjaxResult setModelTop(@PathVariable Long id, @RequestParam boolean isTop) {
        modelService.setModelTop(id, isTop);
        return AjaxResult.success();
    }


    /**
     * 获取置顶的模型列表
     *
     * @return
     */
    @ApiOperation(value = "获取置顶的模型列表")
    @GetMapping
    public R<List<ModelProduct>> fetchModelsSortedByTopStatus() {
        List<ModelProduct> models = modelService.fetchModelsSortedByTopStatus();
        return R.ok(models);
    }

    /**
     * 模型广场
     */
    @ApiOperation(value = "模型广场列表")
    @PostMapping("/modelSquare")
    public R modelSquare(@RequestBody PageVo pageVo) {
        PageInfo<ResponseModelProduct> modelProductPage = modelService.modelSquare(pageVo);
        return R.ok(modelProductPage);
    }

    /**
     * 校验模型名字是否唯一
     * @param name
     * @return
     */
    @ApiOperation(value = "校验模型名字是否唯一")
    @GetMapping("/selectModelByName")
    public R selectModelByName(@RequestParam String name){

        return modelService.selectModelByName(name);
    }

    @ApiOperation(value = "个人中心更改背景")
    @GetMapping("/updateBackgroundImg")
    public R updateBackgroundImg(@RequestParam Long id,String path){

        sysUserService.updateBackgroundImg(id,path);

        return R.ok();
    }

}
