package com.hzs.system.base.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.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.BdStorehouseAreaMiddle;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdStorehouseAreaMiddleService;
import com.hzs.system.base.service.IBdStorehouseService;
import com.hzs.system.base.vo.BdStorehouseAreaMiddleVO;
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;

/**
 * <p>
 * 仓库地区中间表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-05-05
 */
@RestController
@RequestMapping("/manage/storehouse-area-middle")
public class BdStorehouseAreaMiddleController extends BaseController {
    @Autowired
    private IBdStorehouseService iBdStorehouseService;
    @Autowired
    private IBdStorehouseAreaMiddleService iBdStorehouseAreaMiddleService;


    /**
     * 获取地区仓库列表
     */
    @Log(module = EOperationModule.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        startPage();
        storehouseAreaMiddleVO.setPkCountry(SecurityUtils.getPkCountry());
        List<BdStorehouseAreaMiddleVO> list = iBdStorehouseAreaMiddleService.getBdStorehouseAreaMiddleList(storehouseAreaMiddleVO);
        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.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.SELECT)
    @GetMapping("/geOne/{pkId}")
    public AjaxResult getBdStorehouse(@PathVariable Long pkId) {
        BdStorehouseAreaMiddle storehouseAreaMiddle = iBdStorehouseAreaMiddleService.getById(pkId);
        return AjaxResult.success(storehouseAreaMiddle);
    }

    /**
     * @description: 导出地区仓库
     * @author: zhang jing
     * @date: 2023/5/5 20:16
     * @param: [response, storehouseAreaMiddleVO]
     * @return: void
     **/
    @Log(module = EOperationModule.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.SELECT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        List<BdStorehouseAreaMiddleVO> list = iBdStorehouseAreaMiddleService.getBdStorehouseAreaMiddleList(storehouseAreaMiddleVO);
        ExcelUtil<BdStorehouseAreaMiddleVO> util = new ExcelUtil<>(BdStorehouseAreaMiddleVO.class);
        util.exportExcel(response, list, "仓库列表数据");
    }

    /**
     * 新增地区仓库信息
     *
     * @param storehouseAreaMiddleVO
     * @return
     */
    @Log(module = EOperationModule.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        if (StringUtils.isNull(storehouseAreaMiddleVO.getPkStorehouse())) {
            return AjaxResult.error(ConfigMsgConstants.STOREHOUSE_NOT_NULL);
        }
        if (CollectionUtil.isEmpty(storehouseAreaMiddleVO.getProvinceList())) {
            return AjaxResult.error(ConfigMsgConstants.HO_REGION_NOT_NULL);
        }
        Long userId= SecurityUtils.getUserId();
       Integer pkCountry=SecurityUtils.getPkCountry();
       Date date= new Date();
        storehouseAreaMiddleVO.setCreationTime(date);
        storehouseAreaMiddleVO.setPkCreator(userId);
        storehouseAreaMiddleVO.setPkCountry(pkCountry);
        return toAjax(iBdStorehouseAreaMiddleService.intStoArMiddle(storehouseAreaMiddleVO));
    }


    /**
     * 修改保存地区仓库信息
     *
     * @param storehouseAreaMiddleVO
     * @return
     */
    @Log(module = EOperationModule.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdStorehouseAreaMiddleVO storehouseAreaMiddleVO) {
        if (StringUtils.isNull(storehouseAreaMiddleVO.getPkStorehouse())) {
            return AjaxResult.error(ConfigMsgConstants.STOREHOUSE_NOT_NULL);
        }
        if (CollectionUtil.isEmpty(storehouseAreaMiddleVO.getProvinceList())) {
            return AjaxResult.error(ConfigMsgConstants.HO_REGION_NOT_NULL);
        }
        Long userId= SecurityUtils.getUserId();
        Date date= new Date();
        storehouseAreaMiddleVO.setCreationTime(date);
        storehouseAreaMiddleVO.setPkModified(userId);
        return toAjax(iBdStorehouseAreaMiddleService.upStoArMiddle(storehouseAreaMiddleVO));
    }

    /**
     * 删除地区仓库信息
     *
     * @param pkId
     * @return
     */
    @Log(module = EOperationModule.STOREHOUSE, business = EOperationBusiness.STOREHOUSE_AREA_MIDDLE, method = EOperationMethod.DELETE)
    @PostMapping("/del/{pkId}")
    public AjaxResult delBdStorehouse(@PathVariable Long pkId) {
        UpdateWrapper<BdStorehouseAreaMiddle> 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(iBdStorehouseAreaMiddleService.update(updateWrapper));
    }





}
