package com.steerinfo.pretrack.pretrackscale.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkprewgttype.mapper.MeterWorkPreWgttypeMapper;
import com.steerinfo.meterwork.meterworkprewgttype.model.MeterWorkPreWgttype;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.pretrack.pretrackscale.service.IPreTrackScaleService;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
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.web.bind.annotation.*;

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

/**
 * PreTrackScale RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-10-20 09:26
 * 类描述
 * 修订历史：
 * 日期：2020-10-20
 * 作者：generator
 * 参考：
 * 描述：PreTrackScale RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/pretrackscales")
@Api(value = "/${api.version}/pretrackscales", tags = "汽车衡计量预报 - PreTrackScaleController")
public class PreTrackScaleController extends BaseRESTfulController {

    @Autowired
    IPreTrackScaleService preTrackScaleService;

    @Resource
    PreTrackScaleMapper preTrackScaleMapper;

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

    @ApiOperation(value = "C#端获取列表", notes = "根据车号查询委托信息，车号参数和查询的委托信息为空返回空")
    @GetMapping(value = "/findCarScale")
    public RESTfulResult findCarScale(@RequestParam HashMap params) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.findCarScale(params);
            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("pretrackscale:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.queryLikeForPage(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("pretrackscale:view")
    @GetMapping(value = "/likeByDesc")
    public RESTfulResult likeByDesc(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.likeByDesc(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "web端-计量委托倒序获取列表", notes = "分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    @GetMapping(value = "/likeByList/")
    public RESTfulResult likeByList(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.likeByList(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")
    })
    @GetMapping(value = "/webLikeByDesc")
    public RESTfulResult webLikeByDesc(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.webLikeByDesc(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "web端--汽车衡一次、结净数据界面获取委托未过期的倒序列表", notes = "不分页模糊查询")
    @PostMapping(value = "/noPageDesc")
    public RESTfulResult noPageDesc(@RequestBody HashMap parmas) {
        try {
            List<PreTrackScale> list = preTrackScaleService.noPageDesc(parmas);
            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("pretrackscale:view")
    @PostMapping(value = "/like/")
    public RESTfulResult listLikePost(@RequestBody HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<PreTrackScale> list = preTrackScaleService.queryLikeForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

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

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

    @ApiOperation(value = "车号获取", notes = "根据车号获取预报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "Integer")
    })
    @GetMapping(value = "/select")
    public RESTfulResult select(@RequestParam String carNo, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<PreTrackScale> rows = preTrackScaleService.selectlist(carNo);
            PageList pageInfo = new PageList(rows);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        }
        return rm;
    }


    @ApiOperation(value = "车号获取", notes = "根据车号获取预报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "Integer")
    })
    @PostMapping(value = "/queryByFlag")
    public RESTfulResult selectlist(@RequestBody PreTrackScale pre) {
        RESTfulResult rm = failed();
        Map<String, Object> map = FieldsCollector.beanToMap(pre);

        try {
            List<PreTrackScale> rows = preTrackScaleMapper.selectBy(map);
            rm.setCode("200");
            rm.setMessage("操作成功");
            rm.setSucceed();
            rm.setData(rows);
            return rm;
        } 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 = "查询页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "Integer")
    })
    @GetMapping(value = "/selectView")
    public RESTfulResult selectView(@RequestParam HashMap param, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<PreTrackScale> rows = preTrackScaleService.selectView(param);
            PageList pageInfo = new PageList(rows);
            return success(pageInfo);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "新增数据", notes = "根据PreTrackScale对象创建")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/add")
    public RESTfulResult chechandadd(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            PreTrackScale pts = preTrackScaleService.checkandadd(model);
            if (pts != null) {
                return success(pts);
            } 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 = "根据PreTrackScale对象创建")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/update")
    public RESTfulResult chechandupdate(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            PreTrackScale pts = preTrackScaleService.checkandupdate(model);
            if (pts != null) {
                return success(pts);
            } 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 = "根据PreTrackScale对象创建")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/setPreIndex")
    public RESTfulResult setPreIndex(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            if (model == null) {
                return failed("参数为空");
            }
            if (model.getPredictionNo() == null || "".equals(model.getPredictionNo())) {
                return failed("数据预报编号为空");
            }
            if (model.getCarNo() == null || "".equals(model.getCarNo())) {
                return failed("数据预报编号为空");
            }
            int num = preTrackScaleService.setPreIndex(model);
            if (num > 0) {
                return success(num);
            }
            else if(num == -1){
                return failed("已经存在已使用的委托，不可修改");
            }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 = "根据PreTrackScale对象停用")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.abolish(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 = "根据PreTrackScale对象启用")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/recover")
    public RESTfulResult recover(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.recover(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 = "根据预报类型为联运的PreTrackScale对象联运")
    @ApiImplicitParam(name = "models", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/combined")
    public RESTfulResult combined(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.combined(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 = "根据预报类型为联运的PreTrackScale对象解除")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/remove")
    public RESTfulResult remove(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.remove(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 = "Winform获取列表", notes = "分页查询")
    @GetMapping(value = "/doQueryWf", produces = "application/json;charset=UTF-8")
    public RESTfulResult doQueryWf(@RequestParam HashMap parmas) {
        RESTfulResult rm = preTrackScaleService.doQueryWf(parmas);
        return rm;
    }

    @ApiOperation(value = "新增数据", notes = "根据PreTrackScale对象创建")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/addList")
    public RESTfulResult addList(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.addList(models);
            if (num > 0) {
                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 = "APP使用，根据车号获取预报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "Integer")
    })
    @PostMapping(value = "/selectByCarNo")
    public RESTfulResult selectByCarNo(@RequestBody HashMap model) {
        RESTfulResult rm = failed();
        try {
            Integer pageNum =model.get("pageNum") == null ?1: Integer.parseInt(model.get("pageNum").toString());
            Integer pageSize =model.get("pageSize") == null ?20: Integer.parseInt(model.get("pageSize").toString());
            PageHelper.startPage(pageNum,pageSize);
            List<PreTrackScale> rows = preTrackScaleService.selectByCarNo(model.get("carNo").toString(), model.get("valueFlag").toString());
            return success(rows);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "车号获取", notes = "根据车号获取预报信息")
    @PostMapping(value = "/selectByCar")
    public RESTfulResult selectByCar(@RequestBody HashMap model) {
        RESTfulResult rm = failed();
        try {
            List<PreTrackScale> rows = preTrackScaleService.selectByCar(model.get("carNo").toString());
            return success(rows);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "汽车衡新一次补录界面的委托显示", notes = "根据车号获取预报信息")
    @PostMapping(value = "/selectInUseAndNoUse")
    public RESTfulResult selectInUseAndNoUse(@RequestBody HashMap model) {
        RESTfulResult rm = failed();
        try {
            List<PreTrackScale> rows = preTrackScaleService.selectInUseAndNoUse(model);
            return success(rows);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        }
        return rm;
    }
}
