package com.unis.base.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.entity.DCfxxsjyDO;
import com.unis.base.server.service.DCfxxsjyService;
import com.unis.common.utils.FieldUtil;
import com.unis.common.utils.JxlModelExcelUtil;
import com.unis.common.utils.ResponseWrapper;
import com.unis.common.vo.base.BaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangxing
 * @version 1.0
 * @Title DCfxxsjyController
 * @ProjectName 仓房信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "仓房信息", tags = "仓房信息维护")
@Slf4j
public class WarehouseController {

	@Autowired
	private DCfxxsjyService dCfxxsjyService;

    /**
     * @return 仓房信息列表
     * @author huangxing
     * @Description 获取仓房列表
     */
    @ApiOperation(value = "获取仓房信息", notes = "参数：仓房编号，仓房编码之一")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cfbh", value = "仓房编号", dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "cfbm", value = "仓房编码", dataType = "String", paramType = "header")
    })
    @PostMapping(value = "/warehouse/selectById")
    public ResponseWrapper selectById(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            return dCfxxsjyService.selectById(dCfxxsjyVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 仓房信息列表
     * @author huangxing
     * @Description 获取仓房列表
     */
    @ApiOperation(value = "获取仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehouseList")
    public ResponseWrapper getDCfxxxsjyList(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyDOs = dCfxxsjyService.getDCfxxxsjyList(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 仓房信息列表
     * @author huangxing
     * @Description 获取仓房列表
     */
    @ApiOperation(value = "获取仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehouseListCbl")
    public ResponseWrapper getDCfxxxsjyListCbl(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyDOs = dCfxxsjyService.getDCfxxxsjyListCbl(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 仓房信息列表
     * @author huangxing
     * @Description 获取仓房列表
     */
    @ApiOperation(value = "获取成品粮仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehouseChengPinList")
    public ResponseWrapper warehouseChengPinList(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyDOs = dCfxxsjyService.getChengPinList(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 仓房信息列表
     * @author huangxing
     * @Description 获取仓房列表
     */
    @ApiOperation(value = "获取原粮仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehouseYuanLiangList")
    public ResponseWrapper warehouseYuanLiangList(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<BaseKcVO> dCfxxsjyDOs = dCfxxsjyService.getYuanLiangList(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "获取仓房信息列表new", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehouseListNew")
    public ResponseWrapper warehouseListNew(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyDOs = dCfxxsjyService.warehouseListNew(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param dCfxxsjyVO 社会统一行政编码，企业名称，企业类型，企业性质，法定代表人
     * @return 企业信息列表
     * @author huangxing
     * @Description 获取企业信息列表
     */
    @ApiOperation(value = "分页获取仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
    @PostMapping(value = "/warehousePage/{pageNum}/{pageSize}")
    public ResponseWrapper getDCfxxxsjyPage(@RequestBody DCfxxsjyVO dCfxxsjyVO, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            //多表关联 分页失效，查询总条数
            List<DCfxxsjyVO> dCfxxxsjyList = dCfxxsjyService.getDCfxxxsjyList(dCfxxsjyVO);
            PageInfo<DCfxxsjyVO> pageInfos = new PageInfo<>(dCfxxsjyService.getDCfxxxsjyPage(dCfxxsjyVO, pageNum, pageSize));
            if (dCfxxxsjyList != null) {
                pageInfos.setTotal(dCfxxxsjyList.size());
            }
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }


    @PostMapping(value = "/selectDCfxxxsjyPage/{pageNum}/{pageSize}")
    public ResponseWrapper selectDCfxxxsjyPage(@RequestBody DCfxxsjyVO dCfxxsjyVO, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyService.selectDCfxxxsjyPage(dCfxxsjyVO, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    //仓房信息导出
    @ApiOperation(value = "仓房信息导出")
    @PostMapping(value = "/exportDCfxxxsjyPage")
    public void exportDCfxxxsjyPage(HttpServletRequest request, HttpServletResponse response
            , @RequestBody DCfxxsjyVO dCfxxsjyVO
            , @RequestParam(value = "fields", required = false) String fields) throws IOException {
        PageResult<DCfxxsjyVO> pageResult = dCfxxsjyService.selectDCfxxxsjyPage(dCfxxsjyVO, 1, Integer.MAX_VALUE);
        if (pageResult != null && pageResult.getItems() != null) {
            List dataList = pageResult.getItems();
            //fileName导出文件名
            String fileName = "仓房信息.xls";
            //sheetName表格名
            String sheetName = "仓房信息";
            //headMap表头Map, key为属性名, value为展示名,根据实际情况修改
            LinkedHashMap<String, String> headMap = new LinkedHashMap();
            FieldUtil.getPoweredOrderedFields(headMap, new DCfxxsjyVO(), new BaseEntity());
            if (org.apache.commons.lang.StringUtils.isNotEmpty(fields)) {
                FieldUtil.resloveImportFields(fields, headMap);
            }
            JxlModelExcelUtil.exportExcel(request, response, fileName, dataList, sheetName, headMap);
        }
    }

    /**
     * @return 库点信息
     * @author huangxing
     * @Description 根据仓房编码获取仓房信息
     */
    @ApiOperation(value = "根据仓房编码获取仓房信息", notes = "参数：仓房编号")
    @GetMapping(value = "/warehouse/{cfbh}")
    public ResponseWrapper getDCfxxxsjyInfoBycfbh(@PathVariable("cfbh") String cfbh) {
        try {
            DCfxxsjyVO dCfxxsjyVO = dCfxxsjyService.getDCfxxxsjyInfoBycfbh(cfbh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 库点信息
     * @author huangxing
     * @Description 更新仓房信息
     */
    @ApiOperation(value = "更新仓房信息", notes = "参数：仓房编号")
    @PutMapping(value = "/warehouse")
    public ResponseWrapper update(@RequestBody @Validated DCfxxsjyVO dCfxxsjyVO, BindingResult bindingResult) {
        log.info("update 仓房 params >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {}", JSONObject.toJSONString(dCfxxsjyVO));
        try {
            if (bindingResult.hasFieldErrors()){
                //获取与字段相关的所有错误
                List<FieldError> fieldErrors = bindingResult.getFieldErrors();
                List<String> msgList = new ArrayList<>();
                for (FieldError fieldError : fieldErrors){
                    msgList.add(fieldError.getField() + " : " + fieldError.getDefaultMessage());
                }
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), msgList.toString(), null);
            }
            if(null == dCfxxsjyVO.getSjcr() || dCfxxsjyVO.getSjcr()<=0) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "设计仓容必填且不能小于等于0", null);
            }
            if(dCfxxsjyVO.getHwxxsjyVOList().stream().filter(item->item.getHwrl()==0).findAny().isPresent()) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "货位容量必填且不能小于等于0", null);
            }
            if(dCfxxsjyVO.getHwxxsjyVOList().stream().filter(item->item.getCfmc().matches("暂无||测试||无||空")).findAny().isPresent()) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "请填写正确货位名称", null);
            }
            int x = dCfxxsjyService.updateAll(dCfxxsjyVO);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "该仓房已存在", null);
            } else if (x == -2) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "添加仓房状态失败,货位序号已存在", null);
            } else if (DCfxxsjyVO.UPDATE_RES.INVALID.getValue() == x) {
                return ResponseWrapper.error(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), DCfxxsjyVO.UPDATE_RES.INVALID.getName(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @return 库点信息
     * @author WangZL
     * @Description 更新仓房信息
     */
    @ApiOperation(value = "仓房信息-带状态更新", notes = "参数：仓房编号")
    @PutMapping(value = "/warehouseUpdate")
    public ResponseWrapper warehouseUpdate(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            if (StringUtils.isEmpty(dCfxxsjyVO.getCfbh())) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            int x = dCfxxsjyService.warehouseUpdate(dCfxxsjyVO);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该仓房不存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param kddm 参数：社会行政代码
     * @return 状态
     * @author huangxing
     * @Description 删除企业信息
     */
    @DeleteMapping(value = "/warehouse/{kddm}/{cfxh}")
    @ApiOperation(value = "根据仓房编号删除仓房信息", notes = "参数：仓房编号")
    public ResponseWrapper delete(@PathVariable("kddm") String kddm,@PathVariable("cfxh") String cfxh) {
        try {
            int x = dCfxxsjyService.delete(kddm,cfxh);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败", null);
            } else {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请仓房编号是否正确", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param dCfxxsjyVO 仓房信息实体
     * @return 新增状态
     * @author huangxing
     * @Description 新增仓房信息
     */
    @PostMapping(value = "/warehouse")
    @ApiOperation(value = "新增仓房信息", notes = "参数：仓房名称，苦点代码")
    public ResponseWrapper insert(@RequestBody @Validated DCfxxsjyVO dCfxxsjyVO, BindingResult bindingResult) {
        try {
            if (bindingResult.hasFieldErrors()){
                //获取与字段相关的所有错误
                List<FieldError> fieldErrors = bindingResult.getFieldErrors();
                List<String> msgList = new ArrayList<>();
                for (FieldError fieldError : fieldErrors){
                    msgList.add(fieldError.getField() + " : " + fieldError.getDefaultMessage());
                }
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), msgList.toString(), null);
            }
            if(null == dCfxxsjyVO.getSjcr() || dCfxxsjyVO.getSjcr()<=0) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "设计仓容必填且不能小于等于0", null);
            }
            if(dCfxxsjyVO.getHwxxsjyVOList().stream().filter(item->item.getHwrl()==0).findAny().isPresent()) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "货位容量必填且不能小于等于0", null);
            }
            if(dCfxxsjyVO.getHwxxsjyVOList().stream().filter(item->item.getCfmc().matches("暂无||测试||无||空")).findAny().isPresent()) {
                return ResponseWrapper.success(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), "请填写正确货位名称", null);
            }
            if (StringUtils.isEmpty(dCfxxsjyVO.getKddm()) && StringUtils.isNotBlank(dCfxxsjyVO.getCfxh())) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            int x = dCfxxsjyService.insert(dCfxxsjyVO);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加仓房失败,该仓房已存在", null);
            } else if (x == -1) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加仓房状态失败,该库点不存在", null);
            } else if (x == -2) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加仓房状态失败,货位序号已存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param kddm 库点代码
     * @return 状态
     * @author WangZL
     * @Description 根据库点代码删除仓房信息列表
     */
    @DeleteMapping(value = "/warehouseList/{kddm}")
    @ApiOperation(value = "根据库点代码删除仓房信息列表", notes = "参数：库点代码")
    public ResponseWrapper deleteList(@PathVariable("kddm") String kddm) {
        try {
            int x = dCfxxsjyService.deleteList(kddm);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请库点代码是否正确", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param 库点代码
     * @return 仓房信息列表
     * @author WangZL
     * @Description 根据库点代码获取仓房列表
     */
    @ApiOperation(value = "仓房信息-根据库点代码获取仓房列表", notes = "参数：库点代码")
    @PostMapping(value = "/warehouseListByKddm/{kddm}")
    public ResponseWrapper getWarehouseListByKddm(@PathVariable("kddm") String kddm) {
        try {
            if (kddm == null) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyService.getWarehouseListByKddm(kddm,null);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * @param dCfxxsjyVO 库点代码
     * @return 仓房信息列表
     * @author WangZL, wangruimin
     * @Description 根据库点代码获取仓房列表
     * @version V2
     */
    @ApiOperation(value = "仓房信息-根据库点代码获取仓房列表", notes = "参数：库点代码")
    @PostMapping(value = "/warehouse/selectByEntity")
    public ResponseWrapper selectByEntity(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyService.selectByEntity(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    /**
     * 人员信息-备案主表状态
     *
     * @param dCfxxsjyVO
     * @return
     */
    @PostMapping(value = "/warehouseFiling")
    @ApiOperation(value = "仓房信息-备案主表状态", notes = "参数：仓房编号，是否备案")
    public ResponseWrapper updateFiling(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            if (StringUtils.isEmpty(dCfxxsjyVO.getCfbh()) || dCfxxsjyVO.getSfba() == null) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            int i = dCfxxsjyService.updateFiling(dCfxxsjyVO);
            if (i > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
            } else {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
        }
    }

    @ApiOperation(value = "批量更新仓房信息", notes = "参数：仓房编号")
    @PutMapping(value = "/warehouseBatch")
    @Transactional
    public ResponseWrapper updateBatch(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<String> cfbhs = dCfxxsjyVO.getCfbhs();

            for (String cfbh : cfbhs) {

                dCfxxsjyVO.setCfxh(cfbh);

                int x = dCfxxsjyService.updateDCfxxsjy(dCfxxsjyVO);

                if (x == 0) {
                    return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该仓房不存在", null);
                }

            }

            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "获取仓房信息及货位信息")
    @PostMapping(value = "/getWarehouseDetail")
    public ResponseWrapper getWarehouseDetail(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            DCfxxsjyVO vo = dCfxxsjyService.getWarehouseDetail(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), vo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "保存仓房信息下发结果")
    @PutMapping(value = "/warehouse/updateSendResult")
    public ResponseWrapper updateSendResult(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            int x = dCfxxsjyService.updateDCfxxsjy(dCfxxsjyVO);
            if (x > 0) {
                return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
            } else if (x == 0) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该仓房不存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "分页获取仓房及货位信息列表")
    @PostMapping(value = "/warehousePageNew/{pageNum}/{pageSize}")
    public ResponseWrapper getDCfxxxsjyPageNew(@RequestBody DCfxxsjyVO dCfxxsjyVO, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            PageInfo<DCfxxsjyVO> pageInfos = new PageInfo<>(dCfxxsjyService.getDCfxxxsjyPageNew(dCfxxsjyVO, pageNum, pageSize));
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "获取货位信息")
    @PostMapping(value = "/warehouse/selectCfxxsjy")
    public ResponseWrapper selectCfxxsjy(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
        try {
            List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyService.selectCfxxsjy(dCfxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "仓房信息-根据库点代码获取仓房列表", notes = "参数：库点代码")
    @PostMapping(value = "/warehouseListByKddmGrainType/{kddm}/{grainType}")
    public ResponseWrapper warehouseListByKddmGrainType(@PathVariable("kddm") String kddm,@PathVariable("grainType") String grainType) {
        try {
            if (kddm == null) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyService.getWarehouseListByKddm(kddm,grainType);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "分页获取仓房修改记录", tags = {"仓房信息维护"})
    @PostMapping(value = "warehouse/page_update_log/{pageNum}/{pageSize}")
    public ResponseWrapper pageUpdateLog(@RequestBody DCfxxsjyUpdateLogVO params,
                                         @PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize) {
        try {
            PageInfo<DCfxxsjyUpdateLogVO> pageInfos = new PageInfo<>(dCfxxsjyService.pageUpdateLog(params, pageNum, pageSize));
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }
}
