package com.yida.data.school.transaction.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.apply.EduProduct;
import com.yida.data.common.core.entity.apply.EduProductApply;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.dto.transaction.ListBuyStudentDTO;
import com.yida.data.school.dto.transaction.ProductSelectDTO;
import com.yida.data.school.transaction.service.EduProductApplyService;
import com.yida.data.school.transaction.service.EduProductService;
import com.yida.data.school.vo.transcation.EduProductListVO;
import com.yida.data.school.vo.transcation.EduProductNoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 后台商品管理
 */
@Api(tags = "交易-商品")
@RestController
@RequestMapping("/transaction/product")
public class EduProductController {

    @Resource
    private RedisService redisService;
    @Resource
    private EduProductService eduProductService;
    @Resource
    private EduProductApplyService eduProductApplyService;

//    @Secured({"ROLE_role:agent:normal", "ROLE_role:agent:admin"})
    @ApiOperation("商品分页查询")
    @PostMapping("/listProduct")
    public ResultBean listProduct(@RequestBody ProductSelectDTO dto) {
        Page page = new Page();
        page.setCurrent(dto.getCurrent());
        page.setSize(dto.getSize());
        return ResultBean.buildSuccess(eduProductService.selectPageEduProductService(page, dto));
    }

    @ApiOperation("查询商品详情")
    @GetMapping("/selectProduct")
    public ResultBean<EduProduct> selectProduct(@ApiParam("商品id") @RequestParam Long productId) {
        return ResultBean.buildSuccess(eduProductService.getInfo(productId));
    }

    @ApiOperation("修改商品")
    @PostMapping("/upDateProduct")
    @OperationLog(module = ModuleName.PRODUCT, methods = "修改商品", type = OperationLogTypeEnum.INSERT)
    public ResultBean upDataProduct(@ApiParam("商品") @RequestBody EduProduct eduProduct) {
        Asserts.isTrue(eduProductService.count(Wrappers.lambdaQuery(new EduProduct())
                .eq(EduProduct::getProductCode, eduProduct.getProductCode())
                .ne(EduProduct::getProductId, eduProduct.getProductId())) == 0, "商品编码重复，请修改后重试");
        Asserts.isTrue(eduProduct.getProductCode().length() <= 10, "商品编码超过最大长度，请修改后重试");
        eduProductService.upDataEduProductService(eduProduct);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("商品上架")
    @GetMapping("/upDateProductById")
    @OperationLog(module = ModuleName.PRODUCT, methods = "商品上架", type = OperationLogTypeEnum.UPDATE)
    public ResultBean upDateProductById(@ApiParam("商品") @RequestParam("id") Long id) {
        EduProduct eduProduct = new EduProduct();
        eduProduct.setProductId(id);
        eduProduct.setStatus(1);
        eduProductService.upDateProductById(eduProduct);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("下架商品")
    @GetMapping("/soldOutProduct")
    @OperationLog(module = ModuleName.PRODUCT, methods = "下架商品", type = OperationLogTypeEnum.UPDATE)
    public ResultBean soldOutProduct(@ApiParam("商品") @RequestParam Long productId) {
        EduProduct eduProduct = new EduProduct();
        eduProduct.setProductId(productId);
        eduProduct.setStatus(3);
        //商品下架，删除定时催缴job
        eduProductService.soldOutProduct(eduProduct);
        redisService.del(FebsConstant.EDU_PRODUCT + productId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("新增商品")
    @PostMapping("/insertProduct")
    @OperationLog(module = ModuleName.PRODUCT, methods = "新增商品", type = OperationLogTypeEnum.INSERT)
    public ResultBean insertProduct(@RequestBody EduProduct eduProduct) {
        Asserts.isTrue(eduProductService.count(Wrappers.lambdaQuery(new EduProduct())
                .eq(EduProduct::getProductCode, eduProduct.getProductCode())) == 0, "商品编码重复，请修改后重试");
        Asserts.isTrue(eduProduct.getProductCode().length() <= 10, "商品编码超过最大长度，请修改后重试");
        eduProductService.insertEduProductService(eduProduct);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除商品")
    @GetMapping("/removeProduct")
    @OperationLog(module = ModuleName.PRODUCT, methods = "删除商品", type = OperationLogTypeEnum.DELETE)
    public ResultBean<EduProduct> removeProduct(@ApiParam("商品id") @RequestParam Long productId) {
        eduProductService.removeEduProductService(productId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("手机端商品列表查询")
    @GetMapping("/selectProductList")
    public ResultBean<EduProductListVO> listApply(@ApiParam("学校id") @RequestParam Long schoolId) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        assert currentUser != null;
        return ResultBean.buildSuccess(eduProductService.selectProductList(FebsUtil.getCurrentUser().getMobile(), schoolId));
    }

    @ApiOperation("手机商品详情")
    @GetMapping("/selectProductAndApply")
    public ResultBean<EduProduct> selectProductAndApply(@ApiParam("商品id") @RequestParam Long productId) {
        return ResultBean.buildSuccess(eduProductService.selectProductAndApply(productId));
    }

    @ApiOperation("商品与应用取消关联")
    @GetMapping("/delEduProductApply")
    @OperationLog(module = ModuleName.PRODUCT, methods = "商品与应用取消关联", type = OperationLogTypeEnum.DELETE)
    public ResultBean delEduProductApply(@ApiParam("应用id") @RequestParam Long applyId) {

        eduProductApplyService.remove(Wrappers.lambdaQuery(new EduProductApply())
                .eq(EduProductApply::getApplyId, applyId));
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询包含单个类别的商品列表")
    @GetMapping("/listOneCategoryProduct")
    public ResultBean<List<EduProduct>> listOneCategoryProduct(@ApiParam("商品一级类别") String firstCategory,
                                                               @ApiParam("商品二级类别") String secondCategory,
                                                               @ApiParam("学校id") Long schoolId) {
        return ResultBean.buildSuccess(eduProductService.listOneCategoryProduct(firstCategory, secondCategory,
                schoolId));
    }

    @ApiOperation("商品已购买学生列表")
    @GetMapping("/listBuyStudent")
    public ResultBean<Page<EduProductNoticeVO>> listBuyStudent(ListBuyStudentDTO listBuyStudentDTO) {
        Page page = new Page<>();
        page.setSize(listBuyStudentDTO.getPageSize());
        page.setCurrent(listBuyStudentDTO.getPageNum());

        return ResultBean.buildSuccess(eduProductService.listBuyStudent(page, listBuyStudentDTO));
    }

    @ApiOperation("商品未购买学生列表")
    @GetMapping("/listNotBuyStudent")
    public ResultBean<Page<EduProductNoticeVO>> listNotBuyStudent(ListBuyStudentDTO listBuyStudentDTO) {

        Long deptId = FebsUtil.getCurrentUser().getDeptId();
        listBuyStudentDTO.setSchoolId(deptId);
        return ResultBean.buildSuccess(eduProductService.listNotBuyStudent(listBuyStudentDTO));
    }
}
