package com.prolog.eis.controller.shelves;

import com.github.pagehelper.PageHelper;
import com.prolog.eis.common.util.PrologApiJsonHelper;
import com.prolog.eis.dto.item.ItemSyncVO;
import com.prolog.eis.dto.shelves.CategoryAreaDto;
import com.prolog.eis.dto.shelves.FindCategoryAreaDto;
import com.prolog.eis.model.shelves.CategoryAreaNo;
import com.prolog.eis.service.shelves.CategoryAreaNoService;
import com.prolog.eis.service.shelves.ItemService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * WEB接口(货位限定界面)
 *
 */
@Api(tags = "WEB接口(货位限定界面)")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/locationRestricted")
public class LocationLimitedController {

   /* @Resource
    private LocationLimitedService locationLimitedService;*/

    @Resource
    CategoryAreaNoService categoryAreaNoService;

    @Resource
    private ItemService itemService;

    @ApiOperation(value = "货位限定查询", notes = "货位限定查询")
    @PostMapping("/query")
    public RestMessage<Page<CategoryAreaNo>> query(@RequestBody FindCategoryAreaDto dto) throws Exception {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<CategoryAreaNo> categoryAreaNoList = categoryAreaNoService.query(dto);
        Page<CategoryAreaNo> page = PageUtils.getPage(categoryAreaNoList);
        return  RestMessage.newInstance(true ,"查询成功", page);
    }

    @ApiOperation(value = "限定区域查询", notes = "限定区域查询")
    @PostMapping("/queryArea")
    public RestMessage<List<String>> queryArea() throws Exception {
        List<String> areaNos = categoryAreaNoService.queryArea();
        return  RestMessage.newInstance(true ,"查询成功", areaNos);
    }

    @ApiOperation(value = "货位限定保存", notes = "货位限定保存")
    @PostMapping("/save")
    public RestMessage<String> save(@RequestBody CategoryAreaDto dto) throws Exception {
        try {
            categoryAreaNoService.save(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return  RestMessage.newInstance(true, "保存成功");
    }

    @ApiOperation(value = "货位限定修改", notes = "货位限定修改")
    @PostMapping("/update")
    public RestMessage<String> update(@RequestBody CategoryAreaDto dto) throws Exception {
        try {
            Assert.notNull(dto.getId(),"主键不允许为空！");
            categoryAreaNoService.update(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return  RestMessage.newInstance(true, "修改成功");
    }

    @ApiOperation(value = "查询物料信息", notes = "查询物料信息")
    @PostMapping("/queryItem")
    public RestMessage<List<ItemSyncVO>> queryItem(@RequestBody List<String> itemids) throws Exception {
        try {
            List<ItemSyncVO> itemSyncVOS = itemService.queryItemMst(itemids);
            return  RestMessage.newInstance(true, "修改成功",itemSyncVOS);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }

    }

    @ApiOperation(value = "货位限定删除", notes = "货位限定删除")
    @PostMapping("/delete")
    public RestMessage<String> delete(@RequestBody String json) throws Exception {
        PrologApiJsonHelper helper = PrologApiJsonHelper.createHelper(json);
        Long id = helper.getLong("id");
        String areaNo = helper.getString("areaNo");
        try {
            Assert.notNull(id,"主键不允许为空！");
            categoryAreaNoService.delete(id);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return  RestMessage.newInstance(true, "修改成功");
    }



    /*@ApiOperation(value = "货位限定信息查询", notes = "货位限定信息查询")
    @PostMapping("/getLimitedGoodsAllocationInfo")
    public RestMessage<List<LimitedGoodsAllocationInfo>> getLimitedGoodsAllocationInfo(@RequestBody LocationLimitedDto dto) throws Exception {
        List<LimitedGoodsAllocationInfo> list= locationLimitedService.getLimitedGoodsAllocationInfo(dto);
        return RestMessage.newInstance(true,"200", "货位限定信息查询成功", list);
    }

    @ApiOperation(value = "货位限定明细查询", notes = "货位限定明细查询")
    @PostMapping("/getLimitedGoodsAllocationDetails")
    public RestMessage<List<LocationLimitedDetailDto> > getLimitedGoodsAllocationDetails(@RequestBody LocationLimitedDto dto) throws Exception {
        List<LocationLimitedDetailDto>  list = locationLimitedService.getLimitedGoodsAllocationDetails(dto);
        return RestMessage.newInstance(true,"200","查询明细成功",list);
    }


    @ApiOperation(value = "货位限定信息新增", notes = "货位限定信息新增")
    @PostMapping("/insertLimitedGoodsAllocationInfo")
    public RestMessage insertLimitedGoodsAllocationInfo(@RequestBody List<LimitedGoodsAllocationInfo> limitedGoodsAllocationInfo) throws Exception {
        return locationLimitedService.insertLimitedGoodsAllocationInfo(limitedGoodsAllocationInfo);
    }

    @ApiOperation(value = "货位限定信息修改", notes = "货位限定信息修改")
    @PostMapping("/updateLimitedGoodsAllocationInfo")
    public RestMessage updateLimitedGoodsAllocationInfo(@RequestBody List<LimitedGoodsAllocationInfo> limitedGoodsAllocationInfo) throws Exception {
        return locationLimitedService.updateLimitedGoodsAllocationInfo(limitedGoodsAllocationInfo);
    }

    @ApiOperation(value = "货位限定信息明细修改", notes = "货位限定信息明细修改")
    @PostMapping("/updateLimitedGoodsAllocationDetails")
    public RestMessage updateLimitedGoodsAllocationDetails(@RequestBody List<LimitedGoodsAllocationInfoDto> limitedGoodsAllocationInfo) throws Exception {
        return locationLimitedService.updateLimitedGoodsAllocationDetails(limitedGoodsAllocationInfo);
    }

    @ApiOperation(value = "新增货位限定信息明细", notes = "新增货位限定信息明细")
    @PostMapping("/insertLimitedGoodsAllocationDetails")
    public RestMessage insertLimitedGoodsAllocationDetails(@RequestBody List<LimitedGoodsAllocationInfo> limitedGoodsAllocationInfo) throws Exception {
        return locationLimitedService.insertLimitedGoodsAllocationDetails(limitedGoodsAllocationInfo);
    }

    @ApiOperation(value = "货位限定信息删除", notes = "货位限定信息删除")
    @PostMapping("/deleteLimitedGoodsAllocationInfo")
    public RestMessage deleteLimitedGoodsAllocationInfo(@RequestBody LocationLimitedDto dto) throws Exception {
        return locationLimitedService.deleteLimitedGoodsAllocationInfo(dto);
    }
    @ApiOperation(value = "货位限定信息明细删除", notes = "货位限定信息明细删除")
    @PostMapping("/deleteLimitedGoodsAllocationDetails")
    public RestMessage deleteLimitedGoodsAllocationDetails(@RequestBody LocationLimitedDetailDto dto) throws Exception {
        return locationLimitedService.deleteLimitedGoodsAllocationDetails(dto);
    }

    @ApiOperation(value = "货位限定信息导入", notes = "货位限定信息导入")
    @PostMapping("/importLimitedGoodsAllocationInfo")
    public RestMessage<String> importLimitedGoodsAllocationInfo(@RequestPart MultipartFile file) throws Exception {
        return locationLimitedService.importLimitedGoodsAllocationInfo(file);
    }*/
}
