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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
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.BdBarCode;
import com.hzs.common.domain.system.config.BdBarCodeItems;
import com.hzs.common.domain.system.config.ext.BdBarCodeItemsExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.param.BdBarCodeItemsParam;
import com.hzs.system.config.service.IBdBarCodeItemsService;
import com.hzs.system.config.service.IBdBarCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 条形码明细 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/bar-code-items")
public class BdBarCodeItemsController extends BaseController {

    @Autowired
    private IBdBarCodeItemsService barCodeItemsService;
    @Autowired
    private IBdBarCodeService barCodeService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * @description: 查询条形码列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [barCodeItems]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BAR_CODE_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBarCodeItemsExt barCodeItems) {
        startPage();
        List<BdBarCodeItemsExt> list = barCodeItemsService.queryBarCodeItemsList(barCodeItems);
        return getDataTable(list);
    }

    /**
     * @description: 查询条形码列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [barCodeItems]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BAR_CODE_ITEMS, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdBarCodeItemsExt barCodeItems) {
        List<BdBarCodeItemsExt> list = barCodeItemsService.queryBarCodeItemsList(barCodeItems);
        if (CollectionUtil.isNotEmpty(list)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EBarcodeStatus.values());
            for (BdBarCodeItemsExt bi : list) {
                bi.setCodeStatusVal(transactionMap.get(EnumsPrefixConstants.BARCODE_STATUS + bi.getCodeStatus()));
            }
        }
        ExcelUtil<BdBarCodeItemsExt> util = new ExcelUtil<>(BdBarCodeItemsExt.class);
        util.exportExcel(response, list, "条形码明细");
    }


    /**
     * @description: 生成条形码
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [barCodeItems]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BAR_CODE_ITEMS, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdBarCodeItemsParam barCodeItemsParam) {
        List<Long> pkBarCodeList = barCodeItemsParam.getPkBarCode();
        if (CollectionUtil.isNotEmpty(pkBarCodeList)) {
            for (Long pkBarCode : pkBarCodeList) {
                BdBarCode bdBarCode = barCodeService.getOneBarCode(pkBarCode);
                if (StringUtils.isNull(bdBarCode)) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_BAR_CODE_NOT_NULL));
                }
            }
        } else {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_BAR_CODE_NOT_NULL));
        }
        if (StringUtils.isNull(barCodeItemsParam.getGenerateCount()) && barCodeItemsParam.getGenerateCount() > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GENERATE_COUNT_NOT_NULL));
        }
        return toAjax(barCodeItemsService.savBarCode(pkBarCodeList, barCodeItemsParam.getGenerateCount()));
    }


    /**
     * @description: 查询单条条形码
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BAR_CODE_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdBarCodeItems barCodeItems = barCodeItemsService.getById(pkId);
        return AjaxResult.success(barCodeItems);
    }


    /**
     * @description: 删除条形码
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BAR_CODE_ITEMS, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdBarCodeItems> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(barCodeItemsService.update(updateWrapper));
    }


}
