package com.steerinfo.baseinfo.meterbasetermtaredata.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.baseinfo.meterbasetermtaredata.model.MeterBaseTermTareData;
import com.steerinfo.baseinfo.meterbasetermtaredata.service.IMeterBaseTermTareDataService;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.dto.CommonCombination;
import com.steerinfo.meterwork.except.MarkerMetException;
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.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;

/**
 * MeterBaseTermTareData RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-06-24 02:18
 * 类描述
 * 修订历史：
 * 日期：2020-06-24
 * 作者：generator
 * 参考：
 * 描述：MeterBaseTermTareData RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterbasetermtaredatas")
@Api(value = "/${api.version}/meterbasetermtaredatas", tags = "期限皮重数据 - MeterBaseTermTareDataController")
public class MeterBaseTermTareDataController extends BaseRESTfulController {

    @Autowired
    IMeterBaseTermTareDataService meterBaseTermTareDataService;

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

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

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

    @ApiOperation(value = "启用", notes = "根据传过来的meterBaseTermTareData集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meterBaseTermTareData", value = "详细实体meterBaseTermTareData", required = true, dataType = "meterBaseTermTareData")
    })
    @Transactional
    @PostMapping(value = "/Recovery")
    public RESTfulResult Recovery(@RequestBody MeterBaseTermTareData model) {
        try {
            String msg = meterBaseTermTareDataService.Recovery(model);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }


    @ApiOperation(value = "逻辑删除", notes = "根据传过来的meterBaseTermTareData集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meterBaseTermTareData", value = "详细实体meterBaseTermTareData", required = true, dataType = "meterBaseTermTareData")
    })
    @Transactional
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody MeterBaseTermTareData model) {
        try {
            String msg = meterBaseTermTareDataService.abolish(model);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }


    @ApiOperation(value = "根据车号获取汽车衡的皮重数据", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasetermtaredata:view")
    @GetMapping(value = "/selectBy")
    public RESTfulResult selectBy(@RequestParam String carNo, Integer pageNum, Integer pageSize) {
        RESTfulResult rm = failed();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<MeterBaseTermTareData> rows = meterBaseTermTareDataService.selectBy(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 = "C#查询未过期的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meterBaseTermTareData", value = "详细实体meterBaseTermTareData", required = true, dataType = "meterBaseTermTareData")
    })
    @Transactional
    @PostMapping(value = "/doQueryWf")
    public RESTfulResult doQueryWf(@RequestBody MeterBaseTermTareData model) {
        try {
            List<MeterBaseTermTareData> list = meterBaseTermTareDataService.doQueryWf(model);
            return success(list);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "查询失败，请确认！");
        }
    }

    @ApiOperation(value = "C#保存数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meterBaseTermTareData", value = "详细实体meterBaseTermTareData", required = true, dataType = "meterBaseTermTareData")
    })
    @Transactional
    @PostMapping(value = "/doSaveWf")
    public RESTfulResult doSaveWf(@RequestBody CommonCombination<MeterBaseTermTareData, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote> com) {
        try {
            RESTfulResult rm = meterBaseTermTareDataService.doSaveWf(com.getOne(), com.getTwo());
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "保存失败，请确认！");
        }
    }

    @ApiOperation(value = "web端-根据Id手动保存期限皮")
    @Transactional
    @GetMapping(value = "/doSaveTerm")
    public RESTfulResult doSaveTerm(@RequestParam String id) {
        try {
            if(id == null){
                return failed(null, "计量数据编号有问题，请确认！！");
            }
            RESTfulResult rm = meterBaseTermTareDataService.doSaveTerm(id);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            return failed(null, "保存失败，请确认！");
        }
    }

    @ApiOperation(value = "web端-根据Id手动保存期限皮(需要验证留存期限皮权限配置)")
    @Transactional
    @GetMapping(value = "/doSaveTermByConfig")
    public RESTfulResult doSaveTermByConfig(@RequestParam String id) {
        try {
            if(id == null){
                return failed(null, "计量数据编号有问题，请确认！！");
            }
            RESTfulResult rm = meterBaseTermTareDataService.doSaveTermByConfig(id);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            return failed(null, "保存失败，请确认！");
        }
    }

    @ApiOperation(value = "根据车号获取期限皮未失效的数据", notes = "不分页查询")
    @GetMapping(value = "/listBycarNo")
    public RESTfulResult listBycarNo(@RequestParam HashMap parmas) {
        try {
            RESTfulResult rm = meterBaseTermTareDataService.listBycarNo(parmas);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "根据车号获取期限皮未失效的数据显示APP界面", notes = "不分页查询")
    @GetMapping(value = "/ApplistBycarNo")
    public RESTfulResult ApplistBycarNo(@RequestParam HashMap parmas) {
        try {
            RESTfulResult rm = meterBaseTermTareDataService.ApplistBycarNo(parmas);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }
}
