package com.sy.coinage.workshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.constants.CacheKey;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.redis.RedisService;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.workshop.model.Product;
import com.sy.coinage.workshop.model.ProductPlan;
import com.sy.coinage.workshop.req.ProductPlanListReq;
import com.sy.coinage.workshop.req.ProductPlanReq;
import com.sy.coinage.workshop.service.IProductPlanService;
import com.sy.coinage.workshop.service.IProductService;
import com.sy.coinage.workshop.vo.ProductPlanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * -产品计划表Controller
 */
@Api(value = "产品计划表接口", tags = {"产品计划表接口"})
@RestController
@RequestMapping(value = "/productPlan")
public class ProductPlanController extends BaseController {

    @Autowired
    private IProductPlanService productPlanService;

    @Autowired
    private IProductService productService;

    @Autowired
    private RedisService redisService;

    /**
     * 产品计划表分页列表
     */
    @ApiOperation(value = "产品计划表分页列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = ProductPlan.class)
    public IPage<ProductPlan> list(@RequestBody ProductPlanListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<ProductPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(req.getSecurities()), ProductPlan::getSecurities, req.getSecurities());
        queryWrapper.orderByDesc(ProductPlan::getId);
        Page<ProductPlan> page = new Page<>(req.getPage(), req.getRows());
        IPage<ProductPlan> data = this.productPlanService.page(page, queryWrapper);
        return data;
    }

    /**
     * 产品计划表详情
     */
    @ApiOperation(value = "产品计划表-通过ID查询单条数据")
    @PostMapping("/get")
    @LoginNotRequired
    @ActiveFastJsonProfile(groups = "get", clazz = ProductPlan.class)
    public ProductPlanVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        ProductPlanVO data = null;
        ProductPlan model = this.productPlanService.getById(req.getId());
        data = new ProductPlanVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    /**
     * 新增产品计划表
     *
     * @param req {@link ProductPlanReq}
     * @return
     */
    @ApiOperation(value = "新增产品计划表")
    @Operation(value = "新增产品计划表")
    @PostMapping("/add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) ProductPlanReq req,
                      @CurrentUser CacheUserVO user) {
        ProductPlan model = new ProductPlan();
        BeanUtils.copyProperties(req, model);
        Product plan = productService.getByCache(model.getProductId());
        model.setCrownPrefix(plan.getCrownPrefix());
        model.setSecurities(plan.getSecurities());
        model.setStatus(Constants.STATUS_USABLE);
        this.productPlanService.save(model);
        this.deleteCache();
        return "";
    }

    /**
     * 修改产品计划表
     */
    @ApiOperation(value = "修改产品计划表")
    @Operation(value = "修改产品计划表")
    @PostMapping("/update")
    public Object update(@RequestBody @ApiParam(value = "修改请求bean") @Validated({Update.class}) ProductPlanReq req,
                         @CurrentUser CacheUserVO user) {
        ProductPlan model = this.productPlanService.getById(req.getId());
        // 验证是否相同租户
        BeanUtils.copyProperties(req, model);
        Product plan = productService.getByCache(model.getProductId());
        model.setCrownPrefix(plan.getCrownPrefix());
        model.setSecurities(plan.getSecurities());
        this.productPlanService.updateById(model);
        this.deleteCache();
        return "";
    }

    /**
     * 修改产品表状态
     */
    @ApiOperation(value = "修改产品计划表状态")
    @Operation(value = "修改产品计划表状态")
    @PostMapping("/status")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        ProductPlan model = this.productPlanService.getById(req.getId());
        // 验证是否相同租户
        // this.validTenant(user, model.getTenantId());
        if (null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.productPlanService.updateById(model);
        this.deleteCache();
        return model.getStatus();
    }

    private void deleteCache() {
        String key = "Product_Plan";
        this.redisService.del(key);
    }

}
