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


import cn.hutool.core.bean.BeanUtil;
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.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.base.BdProductStorehouse;
import com.hzs.common.domain.system.base.BdProductStorehouseArea;
import com.hzs.common.domain.system.base.BdStorehouseAreaMiddle;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdProductStorehouseAreaService;
import com.hzs.system.base.service.IBdProductStorehouseService;
import com.hzs.system.base.vo.BdProductStorehouseAreaVO;
import com.hzs.system.base.vo.BdProductStorehouseVO;
import com.hzs.system.base.vo.BdStorehouseAreaMiddleVO;
import com.hzs.system.base.vo.BdStorehouseVO;
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;

/**
 * <p>
 * 产品绑定仓库表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2024-04-12
 */
@RestController
@RequestMapping("/manage/bd-product-storehouse")
public class BdProductStorehouseController extends BaseController {

    @Autowired
    private IBdProductStorehouseService iBdProductStorehouseService;
    @Autowired
    private IBdProductStorehouseAreaService iBdProductStorehouseAreaService;
    /**
     * 获取地区仓库列表
     */
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdProductStorehouseVO productStorehouseVO) {
        startPage();
        productStorehouseVO.setPkCountry(SecurityUtils.getPkCountry());
        List<BdProductStorehouseVO> list = iBdProductStorehouseService.getProductStorehouseList(productStorehouseVO);
        list.forEach(itm -> {
            BdProductStorehouseArea psa = new BdProductStorehouseArea();
            psa.setPkProductStorehouse(itm.getPkId());
            psa.setPkCountry(SecurityUtils.getPkCountry());
            List<BdProductStorehouseAreaVO> samList = iBdProductStorehouseAreaService.getStorehouseAreaList(psa);
            itm.setSamList(samList);
        });
        return getDataTable(list);
    }

    /**
     * @description: 获取单条
     * @author: zhang jing
     * @date: 2023/5/5 20:14
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.SELECT)
    @GetMapping("/geOne/{pkId}")
    public AjaxResult getBdStorehouse(@PathVariable Long pkId) {
        BdProductStorehouse productStorehouse = iBdProductStorehouseService.getById(pkId);
        BdProductStorehouseVO ps = BeanUtil.copyProperties(productStorehouse, BdProductStorehouseVO.class);
        BdProductStorehouseArea psa = new BdProductStorehouseArea();
        psa.setPkProductStorehouse(ps.getPkId());
        psa.setPkCountry(SecurityUtils.getPkCountry());
        List<Integer> provinceList = new ArrayList<>();
        List<BdProductStorehouseAreaVO> samList = iBdProductStorehouseAreaService.getStorehouseAreaList(psa);
        if (CollectionUtil.isNotEmpty(samList)) {
            samList.forEach(itm -> provinceList.add(itm.getProvince()));
        }
        ps.setProvinceList(provinceList);
        return AjaxResult.success(ps);
    }

    /**
     * @description: 导出产品绑定仓库
     * @author: zhang jing
     * @date: 2023/5/5 20:16
     * @param: [response, productStorehouseVO]
     * @return: void
     **/
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.SELECT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdProductStorehouseVO productStorehouseVO) {
        productStorehouseVO.setPkCountry(SecurityUtils.getPkCountry());
        List<BdProductStorehouseVO> list = iBdProductStorehouseService.getProductStorehouseList(productStorehouseVO);
        ExcelUtil<BdProductStorehouseVO> util = new ExcelUtil<>(BdProductStorehouseVO.class);
        util.exportExcel(response, list, "产品绑定仓库");
    }

    /**
     * @description: 新增地区产品绑定仓库
     * @author: zhang jing
     * @date: 2024/4/12 10:25
     * @param: [productStorehouseVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdProductStorehouseVO productStorehouseVO) {
        if (StringUtils.isNull(productStorehouseVO.getPkStorehouse())) {
            return AjaxResult.error(ConfigMsgConstants.STOREHOUSE_NOT_NULL);
        }
        if (StringUtils.isNull(productStorehouseVO.getPkProduct())) {
            return AjaxResult.error(ConfigMsgConstants.PK_PRODUCT_NOT_NULL);
        }

        if (CollectionUtil.isEmpty(productStorehouseVO.getProvinceList())) {
            return AjaxResult.error(ConfigMsgConstants.HO_REGION_NOT_NULL);
        }
        Long userId= SecurityUtils.getUserId();
        Integer pkCountry=SecurityUtils.getPkCountry();
        Date date= new Date();
        productStorehouseVO.setCreationTime(date);
        productStorehouseVO.setPkCreator(userId);
        productStorehouseVO.setPkCountry(pkCountry);
        return toAjax(iBdProductStorehouseService.insertProductStorehouse(productStorehouseVO));
    }


    /**
     * @description: 修改保存地区产品绑定仓库
     * @author: zhang jing
     * @date: 2024/4/12 10:25
     * @param: [productStorehouseVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdProductStorehouseVO productStorehouseVO) {
        if (StringUtils.isNull(productStorehouseVO.getPkStorehouse())) {
            return AjaxResult.error(ConfigMsgConstants.STOREHOUSE_NOT_NULL);
        }
        if (CollectionUtil.isEmpty(productStorehouseVO.getProvinceList())) {
            return AjaxResult.error(ConfigMsgConstants.HO_REGION_NOT_NULL);
        }
        Long userId= SecurityUtils.getUserId();
        Date date= new Date();
        productStorehouseVO.setCreationTime(date);
        productStorehouseVO.setPkModified(userId);
        return toAjax(iBdProductStorehouseService.upProductStorehouseVO(productStorehouseVO));
    }

    /**
     * @description: 删除地区产品绑定仓库
     * @author: zhang jing
     * @date: 2024/4/12 10:25
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_STOREHOUSE, business = EOperationBusiness.PRODUCT_STOREHOUSE, method = EOperationMethod.DELETE)
    @PostMapping("/del/{pkId}")
    public AjaxResult delBdStorehouse(@PathVariable Long pkId) {
        return toAjax(iBdProductStorehouseService.delBdStorehouse(pkId));
    }
}
