package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.domain.entity.SysDictData;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.enums.CommonEnums;
import com.yunfeiyun.agmp.common.enums.DictTypeEnums;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.web.system.service.ISysDictDataService;
import com.yunfeiyun.agmp.fms.domain.MppProduct;
import com.yunfeiyun.agmp.fms.service.IMppProductService;
import com.yunfeiyun.agmp.fms.service.IWmsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 产品信息Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-21
 */
@RestController
@RequestMapping("/mpp/product")
public class MppProductController extends BaseController
{
    @Autowired
    private IMppProductService mppProductService;
    @Autowired
    private IWmsGoodsService wmsGoodsService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(MppProduct mppProduct)
    {
        startPage();
        List<MppProduct> list = mppProductService.selectMppProductList(mppProduct);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('mpp:product:select:list')")
    @GetMapping("/select/list")
    public TableDataInfo selectList(MppProduct mppProduct)
    {
        List<MppProduct> list = mppProductService.selectMppProductList(mppProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MppProduct mppProduct)
    {
        List<MppProduct> list = mppProductService.selectMppProductList(mppProduct);
        ExcelUtil<MppProduct> util = new ExcelUtil<MppProduct>(MppProduct.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("productId") String productId)
    {
        return success(mppProductService.selectMppProductByProductId(productId));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody MppProduct mppProduct)
    {
        MppProduct reqVo = new MppProduct();
        reqVo.setProductNameEq(mppProduct.getProductName());
        List<MppProduct> list = mppProductService.selectMppProductList(reqVo);
        if (list.size() > 0) {
            return AjaxResult.error(ErrorCode.FMS_PRODUCT_EXIST.getCode(), ErrorCode.FMS_PRODUCT_EXIST.getMessage());
        }
        mppProduct.setDictCreator(SecurityUtils.getLoginUser().getUsername());

        /* 新增 */
        SysDictData dict = new SysDictData();
        dict.setDictName(mppProduct.getProductName());
        dict.setDictCreator(SecurityUtils.getLoginUser().getUsername());
        dict.setDictCreateddate(DateUtils.dateTimeNow());
        dict.setDicttypeCode(DictTypeEnums.AGM_MPP_PRODUCT.getCode());
        dict.setDictAvlstatus(CommonEnums.DICT_STATUS_NORMAL.getCode());
        //查询当前字典类型的数值的最大值
        Integer max = dictDataService.maxValueByDictType(dict.getDicttypeCode());
        max = max == null ? 0 : max;
        if (StringUtils.isEmpty(dict.getDictValue())) {
            dict.setDictValue(String.valueOf(max+1));
        }

        if (dict.getDictSeq() == null) {
            dict.setDictSeq(Long.valueOf(max+1));
        }
        dict.setDictAvlstatus(CommonEnums.DICT_STATUS_NORMAL.getCode());
        dict.setDictCreator(getUserId());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody MppProduct mppProduct)
    {
        /* 判断产品信息名称不能重复 */
        MppProduct mppProduct1 = mppProductService.selectMppProductByProductId(mppProduct.getProductId());
        if (!mppProduct1.getProductName().equals(mppProduct.getProductName())) {
            MppProduct reqVo = new MppProduct();
            reqVo.setProductNameEq(mppProduct.getProductName());
            List<MppProduct> list = mppProductService.selectMppProductList(reqVo);
            if (list.size() > 0) {
                return AjaxResult.error(ErrorCode.FMS_PRODUCT_EXIST.getCode(), ErrorCode.FMS_PRODUCT_EXIST.getMessage());
            }
        }

        SysDictData dict = new SysDictData();
        dict.setDictId(mppProduct.getProductId());
        dict.setDictName(mppProduct.getProductName());
        dict.setDictModifier(getLoginname());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('mpp:product:delete')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("productIds") String[] productIds)
    {
        return toAjax(mppProductService.deleteMppProductByProductIds(productIds));
    }

}
