package com.steerinfo.meterwork.meterworkcarcheck.controller;

import com.steerinfo.baseinfo.meterbasecalibrastandard.model.MeterBaseCalibraStandard;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcarcheck.model.MeterWorkCarCheck;
import com.steerinfo.meterwork.meterworkcarcheck.service.IMeterWorkCarCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * MeterWorkCarCheck RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-10-19 05:07
 * 类描述
 * 修订历史：
 * 日期：2020-10-19
 * 作者：generator
 * 参考：
 * 描述：MeterWorkCarCheck RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterworkcarchecks")
@Api(value = "/${api.version}/meterworkcarchecks", tags = "车辆核查登记 - MeterWorkCarCheckController")
public class MeterWorkCarCheckController extends BaseRESTfulController {

    @Autowired
    IMeterWorkCarCheckService meterWorkCarCheckService;

    @ApiOperation(value = "获取列表", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkcarcheck:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<MeterWorkCarCheck> list = meterWorkCarCheckService.queryForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "获取列表", notes = "分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkcarcheck:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<MeterWorkCarCheck> list = meterWorkCarCheckService.queryLikeForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "创建", notes = "根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterworkcarcheck:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterWorkCarCheck model) {
        try {
            MeterWorkCarCheck meterWorkCarCheck = meterWorkCarCheckService.add(model);
            return success(meterWorkCarCheck);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkcarcheck:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        try {
            MeterWorkCarCheck meterWorkCarCheck = meterWorkCarCheckService.getById(id);
            return success(meterWorkCarCheck);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的meterWorkCarCheck信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    })
    //@RequiresPermissions("meterworkcarcheck:update")
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterWorkCarCheck model) {
        try {
            model.setId(id);
            MeterWorkCarCheck meterWorkCarCheck = meterWorkCarCheckService.modify(model);
            return success(meterWorkCarCheck);
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkcarcheck:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id) {
        try {
            List<String> list = Arrays.asList(id.split(","));
            if (ListUtils.isNotEmpty(list)) {
                List<String> ids = ListUtils.convertList(list);
                meterWorkCarCheckService.delete(ids);
            }
            return success();
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "获取进厂核查的信息", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkcarcheck:view")
    @GetMapping(value = "/relationIn")
    public RESTfulResult relationIn(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageList<MeterWorkCarCheck> pageInfo = meterWorkCarCheckService.relationIn(parmas, pageNum, pageSize);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "获取出厂核查的信息", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/relationOut")
    public RESTfulResult relationOut(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageList<MeterWorkCarCheck> pageInfo = meterWorkCarCheckService.relationOut(parmas, pageNum, pageSize);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }


    @ApiOperation(value = "进厂核查", notes = "根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterworkcarcheck:create")
    @PostMapping(value = "/incomeAndCheck")
    public RESTfulResult incomeAndCheck(@RequestBody MeterWorkCarCheck model) {
        RESTfulResult rm = failed();
        try {
            MeterWorkCarCheck mwcc = meterWorkCarCheckService.incomeAndCheck(model);
            if (mwcc != null) {
                return success(mwcc);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "出厂核查", notes = "根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterworkcarcheck:create")
    @PostMapping(value = "/outcomeAndCheck")
    public RESTfulResult outcomeAndCheck(@RequestBody MeterWorkCarCheck model) {
        RESTfulResult rm = failed();
        try {
            MeterWorkCarCheck mwcc = meterWorkCarCheckService.outcomeAndCheck(model);
            if (mwcc != null) {
                return success(mwcc);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }


    @ApiOperation(value = "进厂撤销", notes = "根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterworkcarcheck:create")
    @PostMapping(value = "/incomeAndDelete")
    public RESTfulResult incomeAndDelete(@RequestBody List<MeterWorkCarCheck> models) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkCarCheckService.incomeAndDelete(models);
            if (num >= 1) {
                return success(num);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "出厂撤销", notes = "根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "meterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterworkcarcheck:create")
    @PostMapping(value = "/outcomeAndDelete")
    public RESTfulResult outcomeAndDelete(@RequestBody List<MeterWorkCarCheck> models) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkCarCheckService.outcomeAndDelete(models);
            if (num >= 1) {
                return success(num);
            } else {
                return failed("操作失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "WF获取列表")
    @PostMapping(value = "/doQueryWf", produces = "application/json;charset=UTF-8")
    public RESTfulResult doQueryWf(@RequestBody MeterWorkCarCheck oci) {
        try {
            RESTfulResult rm = meterWorkCarCheckService.doQueryWf(oci);
            return rm;
        } catch (Exception ex) {
           ex.printStackTrace();
throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value="创建", notes="根据MeterWorkCarCheck对象创建")
    @ApiImplicitParam(name = "MeterWorkCarCheck", value = "详细实体meterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    //@RequiresPermissions("meterbasecalibrastandard:create")
    @Transactional
    @PostMapping(value = "/insert")
    public RESTfulResult addInsert(@RequestBody MeterWorkCarCheck model){
//        //system.out.println("grjtymkyk,uil"+model);
//        MeterBaseCalibraStandard meterBaseCalibraStandard = meterBaseCalibraStandardService.checkandadd(model);
//
//        return success(meterBaseCalibraStandard);
        try {
            String msg = meterWorkCarCheckService.checkandadd(model);
            if (msg != null && !msg.equals("")) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }


    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的MeterWorkCarCheck信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "MeterWorkCarCheck", value = "详细实体MeterWorkCarCheck", required = true, dataType = "MeterWorkCarCheck")
    })
    //@RequiresPermissions("meterbasecalibrationtime:update")
    @Transactional
    @PutMapping(value = "/update/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult updateBody(@PathVariable String id,@RequestBody MeterWorkCarCheck model) {
        try {
            String msg = meterWorkCarCheckService.checkandupdate(id,model);
            if (msg != null && !msg.equals("")) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }

        return success();
    }
}
