package com.hzs.system.config.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
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.system.config.BdLabel;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.sale.wares.IWaresServiceApi;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdLabelService;
import com.hzs.system.config.vo.BdLabelVO;
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.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/10/20 14:55
 * @Classname: BdLabelController
 * @PackageName: com.hzs.sale.wares.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/label")
public class BdLabelController extends BaseController {

    @Autowired
    private IBdLabelService labelService;

    @DubboReference
    private IWaresServiceApi waresServiceApi;

    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    @Autowired
    ITransactionCommonService iTransactionCommonService;


    @GetMapping("detail")
    public AjaxResult getBdLabelDetail(Integer labelId) {
        BdLabelVO bdLabelVO = BeanUtil.copyProperties(labelService.getByPkId(labelId), BdLabelVO.class);
        bdLabelVO.setLabelTransaction(bdLabelVO.getPkTransaction());
        return AjaxResult.success(bdLabelVO);

    }

    /**
     * 商品标签列表
     *
     * @return AjaxResult
     */
    @PostMapping("list")
    @Log(module = EOperationModule.LABEL_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo list(@RequestBody BdLabelVO bdLabelVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        bdLabelVO.setPkCountry(pkCountry);
        List<BdLabel> bdLabels = labelService.getLabelByPkCountry(bdLabelVO);
        TableDataInfo dataTable = getDataTable(bdLabels);
        List<BdLabelVO> labelVOList = BeanUtil.copyToList(bdLabels, BdLabelVO.class);
        dataTable.setRows(labelVOList);
        return dataTable;
    }

    /**
     * 新增标签
     *
     * @param bdLabelVO
     * @return AjaxResult
     */
    @PostMapping("save")
    @Log(module = EOperationModule.LABEL_SAVE, method = EOperationMethod.INSERT)
    public AjaxResult save(@RequestBody BdLabelVO bdLabelVO) {
        String label = bdLabelVO.getLabel();
        Integer labelType = bdLabelVO.getLabelType();
        String labelImage = bdLabelVO.getLabelImage();
        if (StringUtils.isEmpty(label)) {
            return AjaxResult.error("标签名称不能为空！");
        }
        if (null == labelType) {
            return AjaxResult.error("标签类型不能为空！");
        }
        if (StringUtils.isEmpty(labelImage)) {
            return AjaxResult.error("标签图片不能为空！");
        }
        BdLabel bdLabel = BeanUtil.copyProperties(bdLabelVO, BdLabel.class);
        bdLabel.setPkCountry(SecurityUtils.getPkCountry());
        bdLabel.setPkCreator(SecurityUtils.getUserId());
        R<Integer> pkTransactionKey = iTransactionServiceApi.createTransaction(SecurityUtils.getPkCountry(), ETransactionKey.LABEL,
                label, EYesNo.NO, EYesNo.NO);
        bdLabel.setPkTransaction(pkTransactionKey.getData());
        return toAjax(labelService.save(bdLabel));
    }

    /**
     * 删除标签
     *
     * @param pkId
     * @return
     */
    @GetMapping("delete")
    @Log(module = EOperationModule.LABEL_DELETE, method = EOperationMethod.DELETE)
    public AjaxResult delete(Integer pkId) {
        Boolean isBind = waresServiceApi.checkLabelisUse(pkId).getData();
        if (isBind) {
            return AjaxResult.error("已绑定商品，不允许删除！");
        }
        BdLabel label = labelService.getByPkId(pkId);
        //移除缓存
        iTransactionServiceApi.removeTransactionByPkId(Collections.singletonList(label.getPkTransaction().longValue()));
        return toAjax(labelService.deleteLabelById(pkId));
    }


    /**
     * 编辑标签信息
     *
     * @return
     */
    @PostMapping("update")
    @Log(module = EOperationModule.LABEL_DELETE, method = EOperationMethod.UPDATE)
    public AjaxResult update(@RequestBody BdLabelVO bdLabelVO) {
        String label = bdLabelVO.getLabel();
        Integer labelType = bdLabelVO.getLabelType();
        String labelImage = bdLabelVO.getLabelImage();
        if (StringUtils.isEmpty(label)) {
            return AjaxResult.error("标签名称不能为空！");
        }
        if (null == labelType) {
            return AjaxResult.error("标签类型不能为空！");
        }
        if (StringUtils.isEmpty(labelImage)) {
            return AjaxResult.error("标签图片不能为空！");
        }

        BdLabel checkBdLabel = labelService.getById(bdLabelVO.getPkId());
        if (null == checkBdLabel) {
            return AjaxResult.error("标签不存在");
        }

        BdLabel bdLabel = BeanUtil.copyProperties(bdLabelVO, BdLabel.class);
        bdLabel.setPkModified(SecurityUtils.getUserId());
        bdLabel.setModifiedTime(new Date());
        // 更新翻译
        iTransactionServiceApi.updateTransaction(SecurityUtils.getPkCountry(), label, checkBdLabel.getPkTransaction(), true);
        return AjaxResult.success(labelService.updateLabelById(bdLabel));
    }


    /**
     * 导出
     *
     * @param response
     * @param bdLabelVO
     */
    @PostMapping("/export")
    @Log(module = EOperationModule.LABEL_DELETE, method = EOperationMethod.EXPORT)
    public void export(HttpServletResponse response, BdLabelVO bdLabelVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        bdLabelVO.setPkCountry(pkCountry);
        List<BdLabel> bdLabels = labelService.getLabelByPkCountry(bdLabelVO);
        List<BdLabelVO> labelVOList = BeanUtil.copyToList(bdLabels, BdLabelVO.class);
        //枚举翻译
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ELabelType.values());
        if (CollUtil.isNotEmpty(labelVOList)) {
            for (BdLabelVO labelVO : labelVOList) {
                labelVO.setLabelTypeVal(transactionMap.get(EnumsPrefixConstants.TAG_TYPE + labelVO.getLabelType()));
            }
        }
        ExcelUtil<BdLabelVO> util = new ExcelUtil<>(BdLabelVO.class);
        util.exportExcel(response, labelVOList, "标签导出");
    }

    /**
     * 查询所有标签
     */
    @GetMapping("find-all")
    public AjaxResult findAll(BdLabel bdLabel) {
        QueryWrapper<BdLabel> queryWrapper = new QueryWrapper<>();
        if (bdLabel.getLabelType() != null) {
            queryWrapper.eq("LABEL_TYPE", bdLabel.getLabelType());
        }
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        List<BdLabel> list = labelService.list(queryWrapper);
        return AjaxResult.success(list);
    }
}
