package com.shkj.wms.controller.base;

import java.util.List;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.shkj.common.annotation.Log;
import com.shkj.common.annotation.RepeatSubmit;
import com.shkj.common.config.ShkjConfig;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.utils.poi.ExcelUtil;
import com.shkj.wms.bo.BaseLocationManyBo;
import com.shkj.wms.bo.BaseLocationQueryListBo;
import com.shkj.wms.bo.BaseLocationQueryPageBo;
import com.shkj.wms.bo.BaseLocationSaveOrUpdateBo;
import com.shkj.wms.common.SwitchStatusEntity;
import com.shkj.wms.service.IBaseLocationService;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.vo.BaseLocationExcelVo;
import com.shkj.wms.vo.BaseLocationMapListVo;
import com.shkj.wms.vo.ImportLocationVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.wms.vo.BaseLocationVo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 货位资料管理
 *
 * @menu 货位资料管理
 * @author lch
 * @date 2022-04-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wms/location")
public class BaseLocationController extends BaseController {

    private final IBaseLocationService iBaseLocationService;

    /**
     * 查询货位资料列表
     */
    @GetMapping("/pageBaseLocation")
    public TableDataInfo<BaseLocationVo> pageBaseLocation(BaseLocationQueryPageBo bo) {
        startPage();
        List<BaseLocationVo> list = iBaseLocationService.queryBaseLocationList(bo);
        return getDataTable(list);
    }

//    /**
//     * 根据区域获取对应的货位所具有的巷道信息
//     */
//    @GetMapping("/queryBaseLocationTunnelList")
//    public Result<List<String>> queryBaseLocationTunnelList(BaseLocationQueryListBo bo) {
//        List<String> list = iBaseLocationService.queryBaseLocationTunnelList(bo);
//        return Result.ok().data(list);
//    }

    /**
     * 保存或者修改货位资料（根据是否有id来判断新增或者修改）
     * @param list
     * @return
     */
    @PostMapping("/saveOrUpdateBaseLocations")
    @RepeatSubmit
    public Result<Boolean> saveOrUpdateBaseLocations(@Valid @RequestBody ValidList<BaseLocationSaveOrUpdateBo> list){
        return iBaseLocationService.saveOrUpdateBaseLocations(list);
    }

//    /**
//     * 按规则保存多个货位资料集合
//     * @param bo
//     * @return
//     */
//    @PostMapping("/saveBaseLocationMany")
//    @RepeatSubmit
//    public Result<Boolean> saveBaseLocationMany(@Valid @RequestBody BaseLocationManyBo bo){
//        return iBaseLocationService.saveBaseLocationMany(bo);
//    }


    /**
     * 删除货位资料
     */
    @PostMapping("/removeBaseLocation")
    public Result<Boolean> remove(@Valid @RequestBody ValidList<Long> ids) {
        return iBaseLocationService.deleteWithValidById(ids, true);
    }


    /**
     * 切换状态
     */
    @Log(title = "切换状态" , businessType = BusinessType.UPDATE)
    @PostMapping("/switchStatus")
    public Result<Boolean> switchStatus(@Valid @RequestBody SwitchStatusEntity switchStatusEntity) {
        return iBaseLocationService.switchStatus(switchStatusEntity.getId(), switchStatusEntity.getIsuse());
    }


    /**
     * 下载货位模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BaseLocationExcelVo> util = new ExcelUtil<>(BaseLocationExcelVo.class);
        util.importTemplateExcel(response, "货位模板");
    }

    /**
     * 导入货位数据
     */
    @Log(title = "导入货位数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public Result<ImportLocationVo> importData(MultipartFile file) throws Exception {
        ExcelUtil<BaseLocationExcelVo> util = new ExcelUtil<>(BaseLocationExcelVo.class);
        List<BaseLocationExcelVo> list = util.importExcel(file.getInputStream());
        Result<ImportLocationVo> result = iBaseLocationService.importLocation(list);
        if (!Result.isOk(result)) {
            return result;
        }
        ImportLocationVo data = result.getData();
        if (data.getFailureNumber() > 0) {
            String url = ShkjConfig.getImportPath() + "/" + new SnowflakeGenerator().next() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
            util.deleteRows(file.getInputStream(), data.getIndexList(), url);
            String urlPre = ShkjConfig.getUrlPre();
            data.setFailureUrl((StringUtils.isBlank(urlPre) ? "" : urlPre) + url);
        }
        return result;
    }

    /**
     * 查询货位--map
     */
    @GetMapping("/getBaseLocationForMap")
    public TableDataInfo<BaseLocationMapListVo> getBaseLocationForMap(BaseLocationQueryPageBo bo) {
        startPage();
        List<BaseLocationMapListVo> list = iBaseLocationService.getBaseLocationForMap(bo);
        return getDataTable(list);
    }
}
