package com.huimv.admin.controller.primary;

import com.huimv.admin.domain.primary.ProductionData;
import com.huimv.admin.service.primary.IProductionDataService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.admin.controller
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/1/4 0004 15:01
 **/
@CrossOrigin
@RestController
@RequestMapping("/productionData")
public class ProductionDataController {

    @Autowired
    private IProductionDataService productionDataService;


    @RequestMapping("/add")
    public Result addProductionData (ProductionData productionData){
        try {
            productionDataService.save(productionData);
            return new Result(10000,"添加成功",true);
        }catch (Exception e){
            return new Result(10001,"添加失败",false);
        }
    }

    @RequestMapping("/update")
    public Result updateProductionData (ProductionData productionData){
        try {
            if (productionData.getId() == null){
                return new Result(10001,"请选择要修改的数据",false);
            }
            productionDataService.update(productionData);
            return new Result(10000,"修改成功",true);
        }catch (Exception e){
            return new Result(10001,"修改失败",false);
        }
    }


    @RequestMapping("/delete")
    public Result deleteProductionData (@RequestParam(required = false) List<Integer> ids){
        try {
            if (ids == null || ids.size() <= 0){
                return new Result(10001,"请选择要删除的数据",false);
            }
            productionDataService.delete(ids);
            return new Result(10000,"删除成功",true);
        }catch (Exception e){
            return new Result(10001,"删除失败",false);
        }
    }

    @RequestMapping("/list")
    public Result findProductionData (){
        try {
            List<ProductionData> productionDataList = productionDataService.findAll();
           return new Result(ResultCode.SUCCESS,productionDataList);
        }catch (Exception e){
            return new Result(ResultCode.FAIL,null);
        }
    }

}
