package org.springblade.modules.api.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.StatusEnum;
import org.springblade.common.enums.TrademarkTypeEnum;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.CustomWineOrderSave;
import org.springblade.modules.api.request.SaveDIYInfoRequest;
import org.springblade.modules.api.response.CustomWineResponse;
import org.springblade.modules.api.response.RenderFileResponse;
import org.springblade.modules.api.response.SpecialResponse;
import org.springblade.modules.api.response.SubmitOrderResponse;
import org.springblade.modules.custom.entity.*;
import org.springblade.modules.custom.service.*;
import org.springblade.modules.custom.vo.*;
import org.springblade.modules.custom.wrapper.StickerWrapper;
import org.springblade.modules.custom.wrapper.TrademarkWrapper;
import org.springblade.modules.finance.service.ICustomWineOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Author pangqijun
 * Date 2023/2/13
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/customWine")
@Api(value = "公共接口", tags = "07.定制模块接口")
public class CustomWineController extends BladeController {

    @Autowired
    private IBottleBodyService bottleBodyService;
    @Autowired
    private IBottleCapService bottleCapService;
    @Autowired
    private IBottleLabelService bottleLabelService;
    @Autowired
    private IWineBoxService wineBoxService;
    @Autowired
    private IWineCaseService wineCaseService;
    @Autowired
    private IWineBoxStuffingService wineBoxStuffingService;
    @Autowired
    private IWineCaseStuffingService wineCaseStuffingService;
    @Autowired
    private IBaseWineService baseWineService;
    @Autowired
    private ICustomWineOrderService customWineOrderService;
    @Autowired
    private IBartenderService bartenderService;
    @Autowired
    private IHandbagService handbagService;
    @Autowired
    private IManufacturerService manufacturerService;
    @Autowired
    private IStickerService stickerService;
    @Autowired
    private ITrademarkService trademarkService;
    @Autowired
    private ISpecialBaseWineService specialBaseWineService;
    @Autowired
    private ISpecialFormulaService specialFormulaService;


    @GetMapping("/bottleBody/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取瓶身列表")
    public R<List<BottleBody>> getBottleBodyList(Query query) {
        LambdaQueryWrapper<BottleBody> wrapper = Wrappers.lambdaQuery(BottleBody.class)
                .eq(BottleBody::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleBody::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<BottleBody> page = bottleBodyService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/bottleCap/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取酒盖列表")
    public R<List<BottleCap>> getBottleCapList(Query query) {
        LambdaQueryWrapper<BottleCap> wrapper = Wrappers.lambdaQuery(BottleCap.class)
                .eq(BottleCap::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleCap::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<BottleCap> page = bottleCapService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/bottleCap/byBodyId")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据酒瓶id查询酒盖列表")
    public R<List<BottleCapVO>> getBottleCapList(@RequestParam("bottleBodyId") Long bottleBodyId, Query query) {
        IPage<BottleCapVO> page = bottleCapService.selectByBottleBodyId(Condition.getPage(query), bottleBodyId);
        return data(page.getRecords());
    }

    @GetMapping("/bottleLabel/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取标贴列表")
    public R<List<BottleLabel>> getBottleLabelList(Query query) {
        LambdaQueryWrapper<BottleLabel> wrapper = Wrappers.lambdaQuery(BottleLabel.class)
                .eq(BottleLabel::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleLabel::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<BottleLabel> page = bottleLabelService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/wineBox/list")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "获取酒盒列表")
    public R<List<WineBox>> getWineBoxList(Query query) {
        LambdaQueryWrapper<WineBox> wrapper = Wrappers.lambdaQuery(WineBox.class)
                .eq(WineBox::getStatus, StatusEnum.ENABLE.getValue()).eq(WineBox::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<WineBox> page = wineBoxService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/wineCase/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取酒箱列表")
    public R<List<WineCase>> getWineCaseList(Query query) {
        LambdaQueryWrapper<WineCase> wrapper = Wrappers.lambdaQuery(WineCase.class)
                .eq(WineCase::getStatus, StatusEnum.ENABLE.getValue()).eq(WineCase::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<WineCase> page = wineCaseService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/wineCaseStuffing/list")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取酒箱填充物列表")
    public R<List<WineCaseStuffing>> getWineCaseStuffingList(Query query) {
        LambdaQueryWrapper<WineCaseStuffing> wrapper = Wrappers.lambdaQuery(WineCaseStuffing.class)
                .eq(WineCaseStuffing::getStatus, StatusEnum.ENABLE.getValue()).eq(WineCaseStuffing::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<WineCaseStuffing> page = wineCaseStuffingService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/wineBoxStuffing/list")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取酒盒填充物列表")
    public R<List<WineBoxStuffing>> getWineBoxStuffingList(Query query) {
        LambdaQueryWrapper<WineBoxStuffing> wrapper = Wrappers.lambdaQuery(WineBoxStuffing.class)
                .eq(WineBoxStuffing::getStatus, StatusEnum.ENABLE.getValue()).eq(WineBoxStuffing::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<WineBoxStuffing> page = wineBoxStuffingService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/baseWine/list")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "获取基酒列表")
    public R<List<BaseWine>> getBaseWineList(Query query, BaseWine baseWine) {
        Assert.notNull(baseWine.getManufacturerId(), "酒厂ID不能为空");
        baseWine.setStatus(StatusEnum.ENABLE.getValue());
        QueryWrapper<BaseWine> queryWrapper = Condition.getQueryWrapper(baseWine);
        IPage<BaseWine> page = baseWineService.page(Condition.getPage(query), queryWrapper);
        return data(page.getRecords());
    }

    @PostMapping("/saveOrder")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "定制酒下单接口")
    public R saveOrder(@Valid @RequestBody CustomWineOrderSave customWineOrderSave) throws Exception {
        String order = customWineOrderService.saveOrder(customWineOrderSave, getUser().getUserId());
        return data(new SubmitOrderResponse(order));
    }


    @PostMapping("/save-diy-info")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "保存diy相关信息")
    public R saveDIYInfo(SaveDIYInfoRequest request) {
        Assert.notBlank(request.getOrderNo(), "订单号不能为空");
        customWineOrderService.saveDIYInfo(request);
        return data("保存成功");
    }


    @GetMapping("/getCustomWineData")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "获取瓶身、瓶盖、标贴、酒箱、酒盒列表数据")
    public R<CustomWineResponse> getCustomWineData() {
        CustomWineResponse response = new CustomWineResponse();
        List<BottleBody> bottleBodyList = bottleBodyService.lambdaQuery().eq(BottleBody::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleBody::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        List<BottleCap> bottleCapList = bottleCapService.lambdaQuery().eq(BottleCap::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleCap::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        List<BottleLabel> bottleLabelList = bottleLabelService.lambdaQuery().eq(BottleLabel::getStatus, StatusEnum.ENABLE.getValue()).eq(BottleLabel::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        List<WineCase> wineCaseList = wineCaseService.lambdaQuery().eq(WineCase::getStatus, StatusEnum.ENABLE.getValue()).eq(WineCase::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        List<WineBox> wineBoxList = wineBoxService.lambdaQuery().eq(WineBox::getStatus, StatusEnum.ENABLE.getValue()).eq(WineBox::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        List<Handbag> handbagList = handbagService.lambdaQuery().eq(Handbag::getStatus, StatusEnum.ENABLE.getValue()).eq(Handbag::getIsDelete, YesOrNoEnum.NO.getValue()).list();
        Query query = new Query();
        query.setSize(100);
        IPage<ManufacturerVO> page = manufacturerService.selectManufacturerPage(Condition.getPage(query), new ManufacturerVO());

        response.setBottleBodyList(bottleBodyList);
        response.setBottleCaps(bottleCapList);
        response.setBottleLabels(bottleLabelList);
        response.setWineBoxList(wineBoxList);
        response.setWineCaseList(wineCaseList);
        response.setHandbagList(handbagList);
        response.setManufacturerList(page.getRecords());
        return data(response);
    }



    @GetMapping("/bartender/list")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "获取调酒师列表")
    public R<List<BartenderVO>> getBartenderList(Query query) {
        IPage<BartenderVO> page = bartenderService.getBartenderList(query);
        return data(page.getRecords());
    }

    @GetMapping("/handbag/list")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "获取手提包列表")
    public R<List<Handbag>> getHandbagList(Query query) {
        LambdaQueryWrapper<Handbag> wrapper = Wrappers.lambdaQuery(Handbag.class)
                .eq(Handbag::getStatus, StatusEnum.ENABLE.getValue()).eq(Handbag::getIsDelete, YesOrNoEnum.NO.getValue());
        IPage<Handbag> page = handbagService.page(Condition.getPage(query), wrapper);
        return data(page.getRecords());
    }

    @GetMapping("/manufacturer/list")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取酒厂列表")
    public R<List<ManufacturerVO>> getManufacturerList(Query query) {
        IPage<ManufacturerVO> page = manufacturerService.selectManufacturerPage(Condition.getPage(query), new ManufacturerVO());
        return data(page.getRecords());
    }

    @GetMapping("/manufacturer/detail")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "获取酒厂详情")
    public R<ManufacturerVO> getManufacturerList(Long manufacturerId) {
        ManufacturerVO manufacturerVO = manufacturerService.detail(manufacturerId);
        return data(manufacturerVO);
    }

    @GetMapping("/sticker/list")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "贴纸列表")
    public R<List<StickerVO>> list() {
        List<Sticker> list = stickerService.list();
        return R.data(StickerWrapper.build().listVO(list));
    }

    @GetMapping("/getRenderFile")
    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "查询酒瓶、酒盖3d渲染文件")
    public R<RenderFileResponse> getRenderFile(String bodyId, String capsId, String boxId) {
        RenderFileResponse.RenderFileResponseBuilder builder = RenderFileResponse.builder();
        if (StrUtil.isNotBlank(bodyId)) {
            BottleBody bottleBody = bottleBodyService.getById(bodyId);
            if (bottleBody != null) {
                builder.bodyGlbFile(bottleBody.getModelUrl());
            }
        }
        if (StrUtil.isNotBlank(capsId)) {
            BottleCap bottleCap = bottleCapService.getById(capsId);
            if (bottleCap != null) {
                builder.capsGlbFile(bottleCap.getModelUrl());
            }
        }
        if (StrUtil.isNotBlank(boxId)) {
            WineBox wineBox = wineBoxService.getById(boxId);
            if (wineBox != null) {
                builder.boxGlbFile(wineBox.getModelUrl());
            }
        }
        return R.data(builder.build());
    }


    @GetMapping("/trademark-list")
    @ApiOperationSupport(order = 17)
    @ApiOperation(value = "商标列表", notes = "传入trademark")
    public R<List<TrademarkVO>> trademarkList(@ApiParam("1-平台商标 2-我的商标")  @RequestParam Integer type, Query query) {
        Assert.notNull(type, "类型不能为空");
        LambdaQueryWrapper<Trademark> wrapper = Wrappers.lambdaQuery(Trademark.class);
        if (TrademarkTypeEnum.USER.getValue().equals(type)) {
            wrapper.eq(Trademark::getUserId, AuthUtil.getUserId()).ge(Trademark::getUsePeriod, LocalDateTime.now());
        }
        wrapper.eq(Trademark::getType, type);
        IPage<Trademark> pages = trademarkService.page(Condition.getPage(query), wrapper);
        return R.data(TrademarkWrapper.build().listVO(pages.getRecords()));
    }


    @GetMapping("/special")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "获取酒厂特调信息")
    public R<SpecialResponse> getSpecialInfo(Long manufacturerId) {
        Assert.notNull(manufacturerId, "参数不能为空");
        List<SpecialBaseWine> specialBaseWines = specialBaseWineService.lambdaQuery().eq(SpecialBaseWine::getManufacturerId, manufacturerId).list();
        List<SpecialFormula> specialFormulas = specialFormulaService.lambdaQuery().eq(SpecialFormula::getManufacturerId, manufacturerId).list();
        return data(new SpecialResponse(specialBaseWines, specialFormulas));
    }

    @GetMapping("/bottleLabel/byBodyId")
    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "根据酒瓶id查询标贴列表")
    public R<List<BottleLabelVO>> getBottleLabelList(@RequestParam("bottleBodyId") Long bottleBodyId, Query query) {
        IPage<BottleLabelVO> page = bottleLabelService.selectByBottleBodyId(Condition.getPage(query), bottleBodyId);
        return data(page.getRecords());
    }
}
