package com.hzs.sale.product.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.msg.WaresMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.classify.BdClassify;
import com.hzs.common.domain.sale.product.BdProduct;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.service.IBdClassifyService;
import com.hzs.sale.product.service.IBdProductService;
import com.hzs.sale.product.vo.ClassIfyVo;
import com.hzs.system.base.ITransactionServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 规格分类controller
 */
@Slf4j
@RestController
@RequestMapping("/manage/classify")
public class BdClassifyController extends BaseController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdProductService iBdProductService;
    @Autowired
    private IBdClassifyService iBdClassifyService;

    @DubboReference
    ITransactionServiceApi iTransactionServiceApi;

    /**
     * 查询规格分类
     *
     * @param classIfyVo
     * @return
     */
    @AccessPermissions("cpfl")
    @Log(module = EOperationModule.SPECIFICATIONS_CONFIG, business = EOperationBusiness.PRODUCT_CLASSIFY, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(ClassIfyVo classIfyVo) {
        LambdaQueryWrapper<BdClassify> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(classIfyVo.getName())) {
            queryWrapper.like(BdClassify::getName, classIfyVo.getName());
        }
        if (classIfyVo.getStartCreateDate() != null && classIfyVo.getEndCreateDate() != null) {
            queryWrapper.between(BdClassify::getCreationTime, classIfyVo.getStartCreateDate(), classIfyVo.getEndCreateDate());
        }
        queryWrapper.eq(BdClassify::getPkCountry, SecurityUtils.getPkCountry());
        startPage();
        List<BdClassify> list = iBdClassifyService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 生成规格分类
     *
     * @return
     */
    @Log(module = EOperationModule.CLASSIFY_SAVE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdClassify classify) {
        classify.setPkCreator(SecurityUtils.getUserId());
        classify.setCreationTime(new Date());
        classify.setPkCountry(SecurityUtils.getPkCountry());
        R<Integer> transaction = iTransactionServiceApi.createTransaction(SecurityUtils.getPkCountry(), ETransactionKey.SPEC, classify.getName(), EYesNo.NO, EYesNo.NO);
        classify.setPkTransactionKey(transaction.getData());
        LambdaQueryWrapper<BdClassify> qw = new LambdaQueryWrapper<>();
        qw.eq(BdClassify::getName, classify.getName());
        qw.eq(BdClassify::getPkCountry, SecurityUtils.getPkCountry());
        int count = iBdClassifyService.count(qw);
        if (count > 0) {
            return AjaxResult.error(WaresMsgConstants.CLASSIFICATION_DUPLICATION);
        }
        return AjaxResult.success(iBdClassifyService.save(classify));
    }

    /**
     * 编辑规格分类
     *
     * @return
     */
    @Log(module = EOperationModule.CLASSIFY_UPDATE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdClassify classify) {
        classify.setPkModified(SecurityUtils.getUserId());
        classify.setModifiedTime(new Date());
        classify.setPkCountry(SecurityUtils.getPkCountry());
        String redisKey = CacheConstants.BD_CLASSIFY + classify.getPkId();
        redisService.deleteObject(redisKey);
        return AjaxResult.success(iBdClassifyService.updateById(classify));
    }


    /**
     * 删除规格分类
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.CLASSIFY_DELETE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Integer pkId) {
        LambdaUpdateWrapper<BdClassify> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdClassify::getPkId, pkId);
        updateWrapper.set(BdClassify::getModifiedTime, new Date());
        updateWrapper.set(BdClassify::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdClassify::getDelFlag, EDelFlag.DELETE.getValue());

        String redisKey = CacheConstants.BD_CLASSIFY + pkId;
        redisService.deleteObject(redisKey);
        LambdaQueryWrapper<BdProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(BdProduct::getPkClassify, pkId);
        int count = iBdProductService.count(qw);
        if (count > 0) {
            return AjaxResult.error(WaresMsgConstants.WARES_CLASSIFY_IS_USE);
        }
        return toAjax(iBdClassifyService.update(updateWrapper));
    }

    /**
     * 查询规格分类详情
     *
     * @param classifyId
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Long classifyId) {
        return AjaxResult.success(iBdClassifyService.getById(classifyId));
    }


    /**
     * 修改规格分类状态
     *
     * @return
     */
    @Log(module = EOperationModule.CLASSIFY_UPDATE_ENABLE, method = EOperationMethod.UPDATE)
    @PostMapping("/updateEnable/{pkId}/{enable}")
    public AjaxResult updateEnable(@PathVariable Integer pkId, @PathVariable Integer enable) {
        BdClassify classify = new BdClassify();
        classify.setEnableState(enable);
        classify.setPkId(pkId);
        classify.setPkModified(SecurityUtils.getUserId());
        classify.setModifiedTime(new Date());
        return toAjax(iBdClassifyService.updateById(classify));
    }

    /**
     * 查询所有产品分类
     *
     * @return
     */
    @GetMapping("/find-all")
    public AjaxResult findAll() {
        LambdaQueryWrapper<BdClassify> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdClassify::getPkCountry, SecurityUtils.getPkCountry());
        return AjaxResult.success(iBdClassifyService.list(queryWrapper));
    }

    /**
     * 产品分类导出
     *
     * @param response
     * @param classify
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       BdClassify classify) {
        LambdaQueryWrapper<BdClassify> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(classify.getName())) {
            queryWrapper.eq(BdClassify::getName, classify.getName());
        }
        List<ClassIfyVo> resultList = new ArrayList<>();
        List<BdClassify> list = iBdClassifyService.list(queryWrapper);
        for (BdClassify bdClassify : list) {
            ClassIfyVo classIfyVo = new ClassIfyVo();
            classIfyVo.setCreationTime(bdClassify.getCreationTime());
            classIfyVo.setName(bdClassify.getName());
            resultList.add(classIfyVo);
        }
        ExcelUtil<ClassIfyVo> util = new ExcelUtil<>(ClassIfyVo.class);
        util.exportExcel(response, resultList, "分类导出");

    }
}
