package com.steerinfo.baseinfo.meterbaseoperationlog.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.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaseoperationlog.service.IMeterBaseOperationLogService;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkbeltscaleactual.model.MeterWorkBeltScaleActual;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.PublicMethod;
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.web.bind.annotation.*;

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

/**
 * MeterBaseOperationLog RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2021-03-24 10:30
 * 类描述
 * 修订历史：
 * 日期：2021-03-24
 * 作者：generator
 * 参考：
 * 描述：MeterBaseOperationLog RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterbaseoperationlogs")
@Api(value = "/${api.version}/meterbaseoperationlogs", tags = "计量系统操作日志 - MeterBaseOperationLogController")
public class MeterBaseOperationLogController extends BaseRESTfulController {

    @Autowired
    IMeterBaseOperationLogService meterBaseOperationLogService;

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

    @ApiOperation(value = "获取操作页面列表", notes = "分页查询")
    @PostMapping(value = "/operationPageInfo")
    public RESTfulResult operationPageInfo(@RequestBody(required = false) HashMap parmas) {
        try {
            List<String> list = meterBaseOperationLogService.operationPageInfo(parmas);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "获取操作功能列表", notes = "分页查询")
    @PostMapping(value = "/operationFunctionInfo")
    public RESTfulResult operationFunctionInfo(@RequestBody(required = false) HashMap parmas) {
        try {
            List<String> list = meterBaseOperationLogService.operationFunctionInfo(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("meterbaseoperationlog:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<MeterBaseOperationLog> list = meterBaseOperationLogService.queryLikeForPage(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "创建", notes = "根据MeterBaseOperationLog对象创建")
    @ApiImplicitParam(name = "meterBaseOperationLog", value = "详细实体meterBaseOperationLog", required = true, dataType = "MeterBaseOperationLog")
    //@RequiresPermissions("meterbaseoperationlog:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody MeterBaseOperationLog model) {
        try {
            String id = PublicMethod.IdCreate(2);
            model.setId(id);
            if (model.getCreateTime() == null) {
                model.setCreateTime(new Date());
            }
            if (model.getUpdateManName() != null) {
                model.setUpdateTime(new Date());
            }
            MeterBaseOperationLog meterBaseOperationLog = meterBaseOperationLogService.add(model);
            return success(meterBaseOperationLog);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

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

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


    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "C#端", notes = "根据meterbaseoperationlog对象创建")
    @ApiImplicitParam(name = "MeterBaseOperationLog", value = "详细实体meterbaseoperationlog", required = true, dataType = "meterbaseoperationlog")
    //@RequiresPermissions("meterbaseoperationlog:create")
    @PostMapping(value = "/AddLog")
    public RESTfulResult AddLog(@RequestBody MeterBaseOperationLog model) {
        RESTfulResult rm = failed();
        try {
            int str = meterBaseOperationLogService.doAddLog(model);
            if (str > 0) {
                return success(str);
            } else {
                return failed("操作失败!" + str);
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
            rm.setData(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！" + e.getMessage());
            rm.setData("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    //查询 自定义多条件
    @ApiOperation(value = "C#端皮带秤", notes = "根据MeterBaseOperationLog对象创建")
    @ApiImplicitParam(name = "MeterBaseOperationLog", value = "详细实体MeterBaseOperationLog", required = true, dataType = "MeterBaseOperationLog")
    @PostMapping(value = "/doQueryAndMultiple")
    public RESTfulResult doQueryAndMultiple(@RequestBody CommonPage<MeterBaseOperationLog> model) {
        try {
            List<MeterBaseOperationLog> no = meterBaseOperationLogService.doQueryAndMultiple(model);
            return success(no);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


    @ApiOperation(value = "创建操作日志", notes = "根据传入传出的参数，插入日志数据！")
    @ApiImplicitParam(name = "meterBaseOperationLog", value = "详细实体meterBaseOperationLog", required = true, dataType = "MeterBaseOperationLog")
    @PostMapping(value = "/addComparePropertiesLog")
    public RESTfulResult addComparePropertiesLog(Object source, Object target,MeterBaseOperationLog model) {
        RESTfulResult rm = failed();
        try {
            String str = meterBaseOperationLogService.addComparePropertiesLog(source, target,model);
            if (StringUtils.isEmpty(str)) {
                return success(str);
            } else {
                return failed("操作失败!" + str);
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
            rm.setData(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
            rm.setData("操作异常，请确认！");
            rm.setFailedMessage("异常日志：" + e.getMessage());
        } finally {

        }
        return rm;
    }

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