package com.heketai.website.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.common.result.HttpCode;
import com.heketai.website.entity.ProductCategory;
import com.heketai.website.entity.ProductCategoryAttr;
import com.heketai.website.service.IProductCategoryAttrService;
import com.heketai.website.service.IProductCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类参数表 前端控制器
 * </p>
 *
 * @author xjl
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/productCategoryAttr")
public class ProductCategoryAttrController {
    @Autowired
    IProductCategoryAttrService attrService;
    @Autowired
    IProductCategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    private String fKey = "43AF2D04-4B1F-1DEC-11B9-431BCC8F6E7C";

    @RequestMapping("/op/insert")
    @ApiOperation(value = "新增 商品分类参数表", notes = "新增 商品分类参数表")
    public BaseResult insert(@RequestBody ProductCategoryAttr categoryAttr) {
        if (!StringUtils.isEmpty(categoryAttr.getId())) {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "参数错误!");
        }
        if (StringUtils.isEmpty(categoryAttr.getNameCh()) || StringUtils.isEmpty(categoryAttr.getNameEn())) {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "分类名称不能为空!");
        }

        if (!StringUtils.isEmpty(categoryAttr.getCategoryId())) {
            ProductCategory parent = new ProductCategory();
            parent.setId(categoryAttr.getCategoryId());
            parent = categoryService.selectById(parent);
            if (parent == null) {
                return BaseResult.fail(HttpCode.FAIL.getCode(), "参数错误!");
            }
        } else {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "分类未设置!");
        }

        if (null != categoryAttr.getSort()) {
            ProductCategoryAttr attr = new ProductCategoryAttr();
            attr.setCategoryId(categoryAttr.getCategoryId());
            attr.setSort(categoryAttr.getSort());
            //todo 这里如何设置
            EntityWrapper<ProductCategoryAttr> entity = new EntityWrapper<>();
            entity.setEntity(attr);
            attr = attrService.selectOne(entity);
            if (attr != null) {
                return BaseResult.fail(HttpCode.FAIL.getCode(), "排序已被使用,不可重复!");
            }
        } else {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "排序序号输入!");
        }

        attrService.insert(categoryAttr);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/op/deleteById")
    @ApiOperation(value = "删除 商品分类参数表", notes = "删除 商品分类参数表")
    public BaseResult deleteById(@RequestParam Integer id) {
        attrService.deleteById(id);
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/op/update")
    @ApiOperation(value = "修改 商品分类参数表", notes = "修改 商品分类参数表")
    public BaseResult update(@RequestBody ProductCategoryAttr categoryAttr) {

        if (!StringUtils.isEmpty(categoryAttr.getCategoryId())) {
            ProductCategory parent = new ProductCategory();
            parent.setId(categoryAttr.getCategoryId());
            parent = categoryService.selectById(parent);
            if (parent == null) {
                return BaseResult.fail(HttpCode.FAIL.getCode(), "参数错误!");
            }
        } else {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "分类未设置!");
        }

        if (null != categoryAttr.getSort()) {
            ProductCategoryAttr attr = new ProductCategoryAttr();
            attr.setCategoryId(categoryAttr.getCategoryId());
            attr.setSort(categoryAttr.getSort());
            //todo 这里如何设置
            EntityWrapper<ProductCategoryAttr> entity = new EntityWrapper<>();
            entity.setEntity(attr);
            attr = attrService.selectOne(entity);
            if (attr != null && !attr.getId().equals(categoryAttr.getId())) {
                return BaseResult.fail(HttpCode.FAIL.getCode(), "排序已被使用,不可重复!");
            }
            attrService.updateById(categoryAttr);
        } else {
            return BaseResult.fail(HttpCode.FAIL.getCode(), "排序序号输入!");
        }
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }

    @RequestMapping("/findList")
    @ApiOperation(value = "查询 商品分类参数表", notes = "查询 商品分类参数表")
    public BaseResult findList(@RequestBody ProductCategoryAttr categoryAttr) {
        String key ="AF6C743B557675FA510"+JSON.toJSONString(categoryAttr);
        Object obj = redisTemplate.boundHashOps(fKey).get(key);
        if (null != obj){
            List<ProductCategoryAttr> list = JSON.parseArray(obj.toString(), ProductCategoryAttr.class);
            return BaseResult.successWithData(list);
        }
        EntityWrapper<ProductCategoryAttr> entity = new EntityWrapper<>();
        entity.setEntity(categoryAttr);
        List<ProductCategoryAttr> list = attrService.selectList(entity);

        redisTemplate.boundHashOps(fKey).put(key,JSON.toJSONString(list));
        return BaseResult.successWithData(list);
    }
}
