package com.steerinfo.meterwork.meterworkhookactfirst.controller;

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.dto.CommonCombination;
import com.steerinfo.meterwork.dto.MeterWorkRailwayActFirstDto;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkhookactfirst.model.MeterWorkHookActFirst;
import com.steerinfo.meterwork.meterworkhookactfirst.service.IMeterWorkHookActFirstService;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.pretrack.prehookscale.model.PreHookScale;
import com.steerinfo.pretrack.prerailwayscale.model.PreRailwayScale;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.math.BigDecimal;

/**
 * MeterWorkHookActFirst RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-01-20 03:30
 * 类描述
 * 修订历史：
 * 日期：2022-01-20
 * 作者：generator
 * 参考：
 * 描述：MeterWorkHookActFirst RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/meterworkhookactfirsts")
public class MeterWorkHookActFirstController extends BaseRESTfulController {

    @Autowired
    IMeterWorkHookActFirstService meterWorkHookActFirstService;

    @ApiOperation(value = "获取列表", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkhookactfirst:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<MeterWorkHookActFirst> list = meterWorkHookActFirstService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value = "获取列表", notes = "分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkhookactfirst:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<MeterWorkHookActFirst> list = meterWorkHookActFirstService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }

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

    @ApiOperation(value = "创建", notes = "根据MeterWorkHookActFirst对象创建")
    @ApiImplicitParam(name = "meterWorkHookActFirst", value = "详细实体meterWorkHookActFirst", required = true, dataType = "MeterWorkHookActFirst")
    //@RequiresPermissions("meterworkhookactfirst:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterWorkHookActFirst model) {
        MeterWorkHookActFirst meterWorkHookActFirst = meterWorkHookActFirstService.add(model);
        return success(meterWorkHookActFirst);
    }

    @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkhookactfirst:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        MeterWorkHookActFirst meterWorkHookActFirst = meterWorkHookActFirstService.getById(id);
        return success(meterWorkHookActFirst);
    }

    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的meterWorkHookActFirst信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "meterWorkHookActFirst", value = "详细实体meterWorkHookActFirst", required = true, dataType = "MeterWorkHookActFirst")
    })
    //@RequiresPermissions("meterworkhookactfirst:update")
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterWorkHookActFirst model) {
        model.setId(id);
        MeterWorkHookActFirst meterWorkHookActFirst = meterWorkHookActFirstService.modify(model);
        return success(meterWorkHookActFirst);
    }

    @ApiOperation(value = "删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkhookactfirst:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        if (ListUtils.isNotEmpty(list)) {
            List<String> ids = ListUtils.convertList(list);
            meterWorkHookActFirstService.delete(ids);
        }
        return success();
    }

    @ApiOperation(value = "火车号汇总统计汇总-网页", notes = "根据传入时间和火车号，查询给定时间段去重车号")
    @GetMapping(value = "/distinctCarNo")
    public RESTfulResult distinctCarNo(String railwayNo, String startTime, String endTime) {
        RESTfulResult rm = failed();
        try {
            List<String> rows = meterWorkHookActFirstService.distinctCarNo(railwayNo, startTime, endTime);
            return success(rows);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "新增数据", notes = "根据MeterWorkHookActFirst对象创建")
    @ApiImplicitParam(name = "MeterWorkHookActFirst", value = "详细实体MeterWorkHookActFirst", required = true, dataType = "MeterWorkHookActFirst")
    @PostMapping(value = "/add")
    public RESTfulResult chechandadd(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActFirstService.checkandadd(model);
            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 = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/update")
    public RESTfulResult chechandupdate(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActFirstService.checkandupdate(model);
            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 = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/audit")
    public RESTfulResult audit(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActFirstService.audit(model);
            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 = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/unaudit")
    public RESTfulResult unaudit(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActFirstService.unaudit(model);
            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 = "根据MeterWorkHookActFirst对象修改车号")
    @PostMapping(value = "/updateRailwayNo")
    public RESTfulResult updateRailwayNo(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActFirstService.updateRailwayNo(model);
            return success(num);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "停用-网页", notes = "根据MeterWorkHookActFirst对象修改状态")
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<MeterWorkHookActFirst> models) {
        RESTfulResult rm = failed();
        try {
            int num = 0;
            for(MeterWorkHookActFirst model:models){
                if (!StringUtils.isEmpty(model.getActualFirstNo())){
                    num += meterWorkHookActFirstService.abolish(model);
                }
            }
            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 = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/recover")
    public RESTfulResult recover(@RequestBody List<MeterWorkHookActFirst> models) {
        RESTfulResult rm = failed();
        try {
            int num = 0;
            for(MeterWorkHookActFirst model:models){
                if (!StringUtils.isEmpty(model.getActualFirstNo())){
                    num += meterWorkHookActFirstService.recover(model);
                }
            }
            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("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "匹配委托-网页", notes = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/match")
    public RESTfulResult match(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            MeterWorkHookActFirst mwraf = meterWorkHookActFirstService.match(model.getActualFirstNo(), model.getPredictionNo());
            if (mwraf != null) {
                return success(mwraf);
            } 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 = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/remove")
    public RESTfulResult remove(@RequestBody List<MeterWorkHookActFirst> models) {
        RESTfulResult rm = failed();
        try {
            int num = 0;
            for(MeterWorkHookActFirst model:models){
                if (!StringUtils.isEmpty(model.getActualFirstNo())){
                    num += meterWorkHookActFirstService.remove(model);
                }
            }
            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("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "吊钩秤结净", notes = "根据MeterWorkHookActFirst对象创建")
    @PostMapping(value = "/doAddWfStatic")
    public RESTfulResult doAddWfStatic(@RequestBody MeterWorkHookActFirst model) {
        RESTfulResult rm = failed();
        try {
            String no = meterWorkHookActFirstService.doAddWfStatic(model);
            if (com.steerinfo.util.StringUtils.isEmpty(no)) {
                return failed(null, "新增数据失败");
            }
            return success(no);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "C#端轨道衡实绩静轨结净--两条数据", notes = "根据两个MeterWorkStorageActual对象创建")
    @PostMapping(value = "/doAddWfStaticTwo")
    public RESTfulResult doAddWfStaticTwo(@RequestBody CommonCombination<PreHookScale, MeterWorkHookActFirst, MeterWorkHookActFirst, MeterWorkHookActFirst> com) {
        RESTfulResult rm = failed();
        try {
            rm = meterWorkHookActFirstService.doAddWfStaticTwo(com.getOne(), com.getTwo(), com.getThree());
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value = "动态衡维护界面--毛皮匹配结净保存", notes = "根据毛皮的List<MeterWorkRailwayActFirst>对象进行批量匹配结净保存")
    @PostMapping(value = "/selfServiceMeasurement")
    public RESTfulResult selfServiceMeasurement(@RequestBody MeterWorkHookActFirst first) {
        RESTfulResult rm = failed();
        try {
            if(first != null) {
                rm = meterWorkHookActFirstService.selfServiceMeasurement(first);
            }else {
                rm.setMessage("至少需要穿一条一次数据进行匹配结净保存，请确认！！！！");
            }
            return rm;
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>>" + e.getMessage());
            return rm;
        }
    }
}
