package com.steerinfo.meterwork.meterworkhookactual.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.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkhookactual.model.MeterWorkHookActual;
import com.steerinfo.meterwork.meterworkhookactual.service.IMeterWorkHookActualService;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.math.BigDecimal;

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

    @Autowired
    IMeterWorkHookActualService meterWorkHookActualService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterworkhookactual:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterWorkHookActual> list = meterWorkHookActualService.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("meterworkhookactual:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<MeterWorkHookActual> list = meterWorkHookActualService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据MeterWorkHookActual对象创建")
    @ApiImplicitParam(name = "meterWorkHookActual", value = "详细实体meterWorkHookActual", required = true, dataType = "MeterWorkHookActual")
    //@RequiresPermissions("meterworkhookactual:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterWorkHookActual model){
        MeterWorkHookActual meterWorkHookActual = meterWorkHookActualService.add(model);
        return success(meterWorkHookActual);
    }

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

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

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterworkhookactual: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);
			  meterWorkHookActualService.delete(ids);
    	}
      return success();
    }

    @ApiOperation(value = "新增数据-手输皮重", notes = "根据选中的毛重数据和输入的皮重新增一条净重数据")
    @ApiImplicitParam(name = "MeterWorkHookActual", value = "详细实体MeterWorkHookActual", required = true, dataType = "MeterWorkHookActual")
    @PostMapping(value = "/add")
    public RESTfulResult addData(@RequestBody MeterWorkHookActual model) {
        try {
            int num = meterWorkHookActualService.addData(model);
            if (num >= 1) {
                return success(num);
            } else {
                return failed("操作失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "火车号汇总统计汇总", notes = "根据传入时间和火车号，查询给定时间段去重车号")
    @GetMapping(value = "/distinctCarNo")
    public RESTfulResult distinctCarNo(String railwayNo, String startTime, String endTime) {
        RESTfulResult rm = failed();
        try {
            List<String> rows = meterWorkHookActualService.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 = "将结净数据置为发布状态")
    @ApiImplicitParam(name = "model", value = "详细实体MeterWorkHookActual", required = true, dataType = "MeterWorkHookActual")
    @PostMapping(value = "/publish")
    public RESTfulResult publish(@RequestBody MeterWorkHookActual model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActualService.publish(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 = "将结净数据重置为未发布状态")
    @ApiImplicitParam(name = "model", value = "详细实体MeterWorkHookActual", required = true, dataType = "MeterWorkHookActual")
    @PostMapping(value = "/cancel")
    public RESTfulResult cancel(@RequestBody MeterWorkHookActual model) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActualService.cancel(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 = "结净")
    @PostMapping(value = "/clean")
    public RESTfulResult clean(@RequestParam String actualFirstNo, String dataNo, String predictionNo) {
        RESTfulResult rm = failed();
        try {
            MeterWorkHookActual mwca = meterWorkHookActualService.clean(actualFirstNo, dataNo, predictionNo);
            return success(mwca);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "毛皮解除", notes = "将结净后的数据进行还原（毛皮解除操作）")
    @PostMapping(value = "/remove")
    public RESTfulResult remove(@RequestBody List<MeterWorkHookActual> models) {
        RESTfulResult rm = failed();
        try {
            int num = meterWorkHookActualService.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("操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "匹配委托")
    @PostMapping(value = "/matchPrediction")
    public RESTfulResult matchPrediction(@RequestParam String actualNo, String predictionNo, String note) {
        RESTfulResult rm = failed();
        try {
            MeterWorkHookActual mwca = meterWorkHookActualService.matchPrediction(actualNo, predictionNo, note);
            return success(mwca);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "解除委托")
    @PostMapping(value = "/removePrediction")
    public RESTfulResult removePrediction(@RequestBody MeterWorkHookActual model) {
        RESTfulResult rm = failed();
        try {
            MeterWorkHookActual mwca = meterWorkHookActualService.cleanPrediction(model);
            return success(mwca);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "吊钩秤净重数据补录", notes = "根据MeterWorkHookActual对象创建")
    @PostMapping(value = "/supplementaryData")
    public RESTfulResult supplementaryData(@RequestBody HashMap map) {
        RESTfulResult rm = failed();
        try {
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String grossTimeTemp = map.get("grossTime").toString().replaceAll("-", "/");
            String tareTimeTemp = map.get("tareTime").toString().replaceAll("-", "/");
            String netTimeTemp = map.get("netTime").toString().replaceAll("-", "/");

            Date grossTime = simpleFormat.parse(grossTimeTemp);
            Date tareTime = simpleFormat.parse(tareTimeTemp);
            Date netTime = simpleFormat.parse(netTimeTemp);

            MeterWorkHookActual model = new MeterWorkHookActual();
            model.setPredictionNo(map.get("predictionNo").toString());
            model.setBaseSpot1No(map.get("baseSpot1No").toString());
            model.setBaseSpot1Name(map.get("baseSpot1Name").toString());
            model.setGrossManNo(map.get("grossManNo").toString());
            model.setGrossManName(map.get("grossManName").toString());
            model.setGrossTime(grossTime);
            model.setTareTime(tareTime);
            model.setNetTime(netTime);
            model.setBaseSpot2No(map.get("baseSpot2No").toString());
            model.setBaseSpot2Name(map.get("baseSpot2Name").toString());
            model.setRailwayNo(map.get("railwayNo").toString());
            model.setGrossWeight(new BigDecimal(map.get("grossWeight").toString()));
            model.setTareWeight(new BigDecimal(map.get("tareWeight").toString()));
            model.setRailwayTypeNo(map.get("railwayTypeNo").toString());
            model.setRailwayTypeName(map.get("railwayTypeName").toString());
            rm = meterWorkHookActualService.supplementaryData(model);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }
}
