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


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
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.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.ext.BdBarCodeExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.sale.product.dto.ProductDTO;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdBarCodeService;
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 java.util.Date;
import java.util.List;

/**
 * <p>
 * 条码配置 追溯码设置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-10-17
 */
@Slf4j
@RestController
@RequestMapping("/manage/bd-bar-code")
public class BdBarCodeController extends BaseController {

    @Autowired
    private IBdBarCodeService barCodeService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;
    @DubboReference
    private IProductServiceApi iProductServiceApi;


    /**
     * @description: 查询条形码配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [barCode]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BD_BAR_CODE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBarCodeExt barCodeExt) {
        startPage();
        barCodeExt.setPkCountry(SecurityUtils.getPkCountry());
        List<BdBarCodeExt> list = barCodeService.queryBarCodeList(barCodeExt);
        return getDataTable(list);
    }


    /**
     * @description: 新增条形码配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [barCode]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BAR_CODE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdBarCode barCode) {

        if(StringUtils.isNull(barCode.getPkProduct())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_PRODUCT_NOT_NULL));
        }
        if(StringUtils.isEmpty(barCode.getPrefix())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PREFIX_NOT_NULL));
        }
        if(barCode.getPrefix().length()>2){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PREFIX_DIGIT_ERROR));
        }

        if(StringUtils.isNull(barCode.getIsYear())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_YEAR_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getIsMonth())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_MONTH_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getTailNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAIL_NUMBER_NOT_NULL));
        }
        if(barCode.getTailNumber().toString().length()>9){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAIL_NUMBER_DIGIT_ERROR));
        }
        if(StringUtils.isNull(barCode.getIsInitialize())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_INITIALIZE_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getEndNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.END_NUMBER_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getCurrentNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CURRENT_NUMBER_NOT_NULL));
        }
//        if(StringUtils.isNull(barCode.getTotalBarCode())){
//            return AjaxResult.error(ConfigMsgConstants.TOTAL_BAR_CODE_NOT_NULL);
//        }
//        if(StringUtils.isNull(barCode.getUsedBarCode())){
//            return AjaxResult.error(ConfigMsgConstants.USED_BAR_CODE_NOT_NULL);
//        }

        barCode.setPkProduct(barCode.getPkProduct());
        barCode.setPrefix(barCode.getPrefix());
        barCode.setIsYear(barCode.getIsYear());
        barCode.setIsMonth(barCode.getIsMonth());
        barCode.setTailNumber(barCode.getTailNumber());
        barCode.setIsInitialize(barCode.getIsInitialize());
        barCode.setEndNumber(barCode.getEndNumber());
        barCode.setCurrentNumber(barCode.getCurrentNumber());
        barCode.setTotalBarCode(0);
        barCode.setUsedBarCode(0);
        barCode.setEnableState(barCode.getEnableState());
        barCode.setPkCountry(SecurityUtils.getPkCountry());
        barCode.setPkCreator(SecurityUtils.getUserId());
        barCode.setCreationTime(new Date());
        return toAjax(barCodeService.save(barCode));
    }


    /**
     * @description: 修改条形码配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [barCode]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BAR_CODE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdBarCode barCode) {
        if(StringUtils.isNull(barCode.getPkProduct())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_PRODUCT_NOT_NULL));
        }
        if(StringUtils.isEmpty(barCode.getPrefix())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PREFIX_NOT_NULL));
        }
        if(barCode.getPrefix().length()>2){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PREFIX_DIGIT_ERROR));
        }

        if(StringUtils.isNull(barCode.getIsYear())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_YEAR_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getIsMonth())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_MONTH_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getTailNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAIL_NUMBER_NOT_NULL));
        }
        if(barCode.getTailNumber()>9){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAIL_NUMBER_DIGIT_ERROR));
        }
        if(StringUtils.isNull(barCode.getIsInitialize())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_INITIALIZE_NOT_NULL));
        }
        if(StringUtils.isNull(barCode.getEndNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.END_NUMBER_NOT_NULL));
        }
//        if(StringUtils.isNull(barCode.getEnableState())){
//            return AjaxResult.error(ConfigMsgConstants.ENABLE_STATE_NOT_NULL);
//        }

        barCode.setPkProduct(barCode.getPkProduct());
        barCode.setPrefix(barCode.getPrefix());
        barCode.setIsYear(barCode.getIsYear());
        barCode.setIsMonth(barCode.getIsMonth());
        barCode.setTailNumber(barCode.getTailNumber());
        barCode.setIsInitialize(barCode.getIsInitialize());
        barCode.setEndNumber(barCode.getEndNumber());
        barCode.setEnableState(barCode.getEnableState());
        barCode.setPkCountry(SecurityUtils.getPkCountry());
        barCode.setPkModified(SecurityUtils.getUserId());
        barCode.setModifiedTime(new Date());
        return toAjax(barCodeService.updateById(barCode));
    }


    /**
     * @description: 查询单条条形码配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BAR_CODE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdBarCode barCode = barCodeService.getOneBarCode(pkId);
        BdBarCodeExt bbce = BeanUtil.copyProperties(barCode, BdBarCodeExt.class);
        R<ProductDTO> pdc =iProductServiceApi.getProduct(barCode.getPkProduct());
        if(StringUtils.isNotNull(pdc.getData())){
            bbce.setProductCode(pdc.getData().getProductCode());
            bbce.setProductName(pdc.getData().getProductName());
        }
        return AjaxResult.success(bbce);
    }


    /**
     * @description: 删除条形码配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BAR_CODE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdBarCode> 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(barCodeService.update(updateWrapper));
    }
}
