package com.mvc.controller.produce;

import com.mvc.model.ResultGeekerModel;
import com.mvc.model.produce.DicProductionPartModel;
import com.mvc.model.produce.FilterDataModel;
import com.mvc.service.produce.DicProductionPartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "生产部件字典表")
@RequestMapping("/DicProductionPartAction")
@Controller
public class DicProductionPartAction {

    @Resource
    private DicProductionPartService dicProductionPartService;

    @ApiOperation("查询分类筛选器格式的数据列表")
    @RequestMapping(value = "/findFilterDataModelList/{type}", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findFilterDataModelList(@PathVariable("type") Integer type) {
        ResultGeekerModel resultModel;
        try {
            List<FilterDataModel> res = dicProductionPartService.findFilterDataModelList(type);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/findAllDicProductionPart", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTProductionPlan(@RequestBody DicProductionPartModel model) {
        ResultGeekerModel resultModel;
        try {
            List<DicProductionPartModel> res = dicProductionPartService.findAllDicProductionPart(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增生产部件字典记录")
    @RequestMapping(value = "/saveDicProductionPart", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveDicProductionPart(@RequestBody DicProductionPartModel model) {
        ResultGeekerModel resultModel;
        try {
            DicProductionPartModel resModel = null;
            String error = dicProductionPartService.checkMultipleDataLegitimacy(model);

            if (StringUtils.isNotEmpty(error)) {
                resultModel = new ResultGeekerModel(error);
            } else {
                if (model.getDetails() != null && model.getDetails().size() > 0) {
                    resModel = dicProductionPartService.batchAddDicProductionPart(model);
                } else {
                    resModel = dicProductionPartService.saveDicProductionPart(model);
                }
                resultModel = new ResultGeekerModel(resModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新生产部件字典记录")
    @RequestMapping(value = "/updateDicProductionPart", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateDeviceAcc(@RequestBody DicProductionPartModel model) {
        ResultGeekerModel resultModel;
        try {
            String error = dicProductionPartService.checkDataLegitimacy(model);
            if (StringUtils.isNotEmpty(error)) {
                resultModel = new ResultGeekerModel(error);
            } else {
                DicProductionPartModel resModel = dicProductionPartService.updateDicProductionPart(model);
                resultModel = new ResultGeekerModel(resModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除生产部件字典记录")
    @RequestMapping(value = "/deleteDicProductionPart", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteDicProductionPart(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = dicProductionPartService.deleteDicProductionPart(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
