package com.steerinfo.meterwork.meterworkcarrecover.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcarrecover.model.MeterWorkCarRecover;
import com.steerinfo.meterwork.meterworkcarrecover.service.IMeterWorkCarRecoverService;
import com.steerinfo.util.StringUtils;
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.web.bind.annotation.*;

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

/**
 * MeterWorkCarRecover RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2021-06-09 10:59
 * 类描述
 * 修订历史：
 * 日期：2021-06-09
 * 作者：generator
 * 参考：doRecover
 * 描述：MeterWorkCarRecover RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterworkcarrecovers")
public class MeterWorkCarRecoverController extends BaseRESTfulController {

    @Autowired
    IMeterWorkCarRecoverService meterWorkCarRecoverService;

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

    @ApiOperation(value = "创建", notes = "根据MeterWorkCarRecover对象创建")
    @ApiImplicitParam(name = "meterWorkCarRecover", value = "详细实体meterWorkCarRecover", required = true, dataType = "MeterWorkCarRecover")
    //@RequiresPermissions("meterworkcarrecover:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterWorkCarRecover model) {
        try {
            MeterWorkCarRecover meterWorkCarRecover = meterWorkCarRecoverService.add(model);
            return success(meterWorkCarRecover);
        } 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("meterworkcarrecover:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        try {
            MeterWorkCarRecover meterWorkCarRecover = meterWorkCarRecoverService.getById(id);
            return success(meterWorkCarRecover);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

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

    @ApiOperation(value = "新增", notes = "根据一次计量数据新增复磅的数据")
    @ApiImplicitParam(name = "MeterWorkCarActualFirst", value = "详细一次计量实体MeterWorkCarActualFirst", required = true, dataType = "MeterWorkCarActualFirst")
    @PostMapping(value = "/insert")//String
    public RESTfulResult insert(@RequestBody MeterWorkCarActualFirst first) {
        try {
            String msg = meterWorkCarRecoverService.insert(first);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
            return success();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "修改", notes = "修改复磅的数据")
    @ApiImplicitParam(name = "MeterWorkCarRecover", value = "复磅计量的实体MeterWorkCarRecover", required = true, dataType = "MeterWorkCarRecover")
    @PostMapping(value = "/doUpadte")//String
    public RESTfulResult doUpadte(@RequestBody MeterWorkCarRecover model) {
        try {
            model.setId(model.getRecoverNo());
            model.setUpdateTime(new Date());
            //用户名，用户账户处理
            UserPayload payload = UserPayload.getCurrUser();
            String userCode = payload.getUserCode();
            String userName = payload.getUserName();
            if (model.getUpdateManNo() == null) {
                model.setUpdateManNo(userCode);
                model.setUpdateManName(userName);
            }
            MeterWorkCarRecover meterWorkCarRecover = meterWorkCarRecoverService.modify(model);
            return success(meterWorkCarRecover);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "c#端复磅", notes = "复磅计量")
    @ApiImplicitParam(name = "MeterWorkCarRecover", value = "复磅实体类MeterWorkCarRecover", required = true, dataType = "MeterWorkCarRecover")
    @PostMapping(value = "/doRecover")//String
    public RESTfulResult doRecover(@RequestBody MeterWorkCarRecover model) {
        try {
            String msg = meterWorkCarRecoverService.doRecover(model);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
            return success();
        } catch (MarkerMetException e) {
            e.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        } catch (Exception e) {
            e.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        } finally {

        }
    }
}
