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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.TransactionUtils;
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;

/**
 * @Description: 规格分类controller
 * @Author: yuhui
 * @Time: 2022/8/29 10:59
 * @Classname: BdClassifyController
 * @PackageName: com.hzs.sale.product.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/classify")
public class BdClassifyController extends BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdProductService productService;
    @Autowired
    private IBdClassifyService classifyService;
    @DubboReference
    private ITransactionServiceApi transactionServiceApi;

    /**
     * 查询规格分类
     *
     * @param classIfyVo
     * @return
     */
    @Log(module = EOperationModule.CLASSIFY_LIST, method = EOperationMethod.SELECT)
    @GetMapping("list")
    public TableDataInfo list(ClassIfyVo classIfyVo) {
        QueryWrapper<BdClassify> queryWrapper = new QueryWrapper<BdClassify>();
        if (StringUtils.isNotBlank(classIfyVo.getName())) {
            queryWrapper.like("NAME", classIfyVo.getName());
        }
        if (classIfyVo.getStartCreateDate() != null && classIfyVo.getEndCreateDate() != null) {
            queryWrapper.between("CREATION_TIME", classIfyVo.getStartCreateDate(), classIfyVo.getEndCreateDate());
        }
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        startPage();
        List<BdClassify> list = classifyService.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 = transactionServiceApi.createTransaction(SecurityUtils.getPkCountry(), ETransactionKey.SPEC, classify.getName(), EYesNo.NO, EYesNo.NO);
        classify.setPkTransactionKey(transaction.getData());
        QueryWrapper<BdClassify> qw = new QueryWrapper<>();
        qw.eq("NAME", classify.getName());
        qw.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        int count = classifyService.count(qw);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(WaresMsgConstants.CLASSIFICATION_DUPLICATION));
        }
        return AjaxResult.success(classifyService.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(classifyService.updateById(classify));
    }


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

        String redisKey = CacheConstants.BD_CLASSIFY + pkId;
        redisService.deleteObject(redisKey);
        QueryWrapper<BdProduct> qw = new QueryWrapper<>();
        qw.eq("PK_CLASSIFY", pkId);
        int count = productService.count(qw);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(WaresMsgConstants.WARES_CLASSIFY_IS_USE));
        }
        return toAjax(classifyService.update(updateWrapper));
    }

    /**
     * 查询规格分类详情
     *
     * @param classifyId
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Long classifyId) {
        return AjaxResult.success(classifyService.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(classifyService.updateById(classify));
    }

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

    /**
     * 产品分类导出
     *
     * @param response
     * @param classify
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       BdClassify classify) {
        QueryWrapper<BdClassify> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(classify.getName())) {
            queryWrapper.eq("NAME", classify.getName());
        }
        List<ClassIfyVo> resultList = new ArrayList<>();
        List<BdClassify> list = classifyService.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, "分类导出");

    }
}
