package com.sw.mes.yb.ipc.controller;

import com.sw.mes.base.technology.service.ProcessService;
import com.sw.mes.core.response.ResponsePage;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.DynamicSpecifications;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.init.entity.Device;
import com.sw.mes.base.factory.init.service.DeviceService;
import com.sw.mes.yb.ipc.entity.ParamSet;
import com.sw.mes.yb.ipc.service.ParamSetService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;
import com.sw.mes.base.technology.entity.Process;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("ipc/paramSet")
@Slf4j
@Api(description = "理论工时接口")
public class ParamSetController {

    @Autowired
    private ParamSetService paramSetService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "创建理论工时")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody
    Result create(@RequestBody @Valid ParamSet paramSet) {
        paramSet.setParamName("理论工时");
        paramSetService.saveOrUpdate(paramSet);

        return ResultGenerator.success();
    }

    @ApiOperation(value = "理论工时详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "理论工时ID", paramType = "path", dataType = "Integer"),
    })
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public Result preUpdate(@PathVariable Long id) {
        ParamSet paramSet = paramSetService.get(id);
        return ResultGenerator.success(paramSet);
    }

    @ApiOperation(value = "更新理论工时")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody
    Result update(@Valid @RequestBody ParamSet paramSet) {
        paramSet.setParamName("理论工时");
        paramSetService.saveOrUpdate(paramSet);

        return ResultGenerator.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public @ResponseBody
    Result delete(@PathVariable Long id) {
        paramSetService.delete(id);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "删除理论工时")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "理论工时ID", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Result deleteMany(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            ParamSet paramSet = paramSetService.get(ids[i]);
            paramSetService.delete(paramSet.getId());
        }

        return ResultGenerator.success();
    }

    @ApiOperation(value = "分页查询理论工时列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_processName", value = "工时名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_deviceName", value = "设备类型", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<ResponsePage<ParamSet>> list(ServletRequest request, RequestPage page) {
        Specification<ParamSet> specification = DynamicSpecifications.bySearchFilter(request, ParamSet.class);
        List<ParamSet> paramSets = paramSetService.findByExample(specification, page);

        ResponsePage responsePage = new ResponsePage(paramSets, page.getTotal());

        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "理论工时详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "理论工时ID", paramType = "path", dataType = "Integer"),
    })
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public Result view(@PathVariable Long id) {
        ParamSet paramSet = paramSetService.get(id);
        return ResultGenerator.success(paramSet);
    }

    @ApiOperation(value = "获取所有工序信息")
    @ApiImplicitParams({
    })
    @GetMapping("/getProcess")
    public Result<List<Process>> getProcess() {
        List<Process> processList = processService.findAllAndDel();
        return ResultGenerator.success(processList);
    }

    @ApiOperation(value = "获取所有设备类型信息")
    @ApiImplicitParams({
    })
    @GetMapping("/getDevice")
    public Result<List<Device>> getDevice() {
        List<Device> deviceList = deviceService.findAllAndDel();
        return ResultGenerator.success(deviceList);
    }
}
