package com.jisco.dynamicweight.dynamicworkauditdata.controller;

import cn.hutool.core.lang.Assert;
import com.jisco.dynamicweight.dynamicworkauditdata.model.DynamicWorkAuditdata;
import com.jisco.dynamicweight.dynamicworkauditdata.service.IDynamicWorkAuditdataService;
import com.jisco.dynamicweight.dynamicworkupdata.vo.AuditDataVO;
import com.jisco.dynamicweight.dynamicworkupdata.vo.DailyReportVO;
import com.jisco.dynamicweight.util.ExcelUtil;
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 io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    IDynamicWorkAuditdataService dynamicWorkAuditdataService;

    /**
     * 动态秤报表
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping(value = "/getCollectData/{startTime}/{endTime}")
    public RESTfulResult getCollectData(@PathVariable String startTime, @PathVariable String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf.parse(startTime);
            sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式化错误");
        }
        List<DynamicWorkAuditdata> list = dynamicWorkAuditdataService.getCollectData(startTime, endTime);
        return success(list);
    }

    @ApiOperation(value = "获取列表", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("dynamicworkauditdata:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<DynamicWorkAuditdata> list = dynamicWorkAuditdataService.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("dynamicworkauditdata:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<DynamicWorkAuditdata> list = dynamicWorkAuditdataService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }

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

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

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

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

    @ApiOperation(value = "审核", notes = "动态秤数据审核")
    @ApiImplicitParam(name = "list", value = "待审核数据", required = true, dataType = "List<AuditDataVO>")
    @PostMapping(value = "/audit/{date}")
    public RESTfulResult audit(@PathVariable String date, @RequestBody List<AuditDataVO> list) {
        Assert.isTrue(!CollectionUtils.isEmpty(list), "审核数据不能为空");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式不正确(yyyy-MM-dd)");
        }
        dynamicWorkAuditdataService.audit(date, list);
        return success(null, "操作成功");
    }

    /**
     * 查询上传数据
     *
     * @param auditingTime1
     * @param auditingTime2
     * @return
     */
    @GetMapping(value = "/getUploadData/{auditingTime1}/{auditingTime2}")
    public RESTfulResult getUploadData(@PathVariable String auditingTime1, @PathVariable String auditingTime2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf.parse(auditingTime1);
            sdf.parse(auditingTime2);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式化错误");
        }
        List<DynamicWorkAuditdata> list = dynamicWorkAuditdataService.getUploadData(auditingTime1, auditingTime2);
        return success(list);
    }

    /**
     * 撤销审核的数据
     *
     * @param list
     * @return
     */
    @ApiOperation(value = "撤销审核的数据", notes = "撤销审核的数据")
    @PostMapping(value = "/cancelAudit")
    public RESTfulResult cancelAudit(@RequestBody List<DynamicWorkAuditdata> list) {
        try {
            dynamicWorkAuditdataService.cancelAudit(list);
            return success(null, "审核数据撤销成功!");
        } catch (Exception e) {
            return failed(null, "审核数据撤销失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "导出上传数据到Excel", notes = "导出上传数据到Excel")
    @PostMapping(value = "/exportExcel/{auditingTime1}/{auditingTime2}")
    public void exportUploadDataExcel(HttpServletResponse response, @PathVariable String auditingTime1, @PathVariable String auditingTime2) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf.parse(auditingTime1);
            sdf.parse(auditingTime2);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式化错误");
        }
        List<DynamicWorkAuditdata> list = dynamicWorkAuditdataService.getUploadData(auditingTime1, auditingTime2);
        ExcelUtil.exportExcel(response, list, DynamicWorkAuditdata.class, "上传数据");
    }

    @ApiOperation(value = "撤销上传的数据", notes = "撤销上传的数据")
    @PostMapping(value = "/cancelUpload")
    public RESTfulResult cancelUpload(@RequestBody List<DynamicWorkAuditdata> list) {
        try {
            dynamicWorkAuditdataService.cancelUpload(list);
            return success(null, "撤销上传数据成功!");
        } catch (Exception exception) {
            System.err.println(exception.getMessage());
            return failed(null, exception.getMessage());
        }
    }

    @ApiOperation(value = "数据上传", notes = "数据上传")
    @PostMapping(value = "/upload")
    public RESTfulResult upload(@RequestBody List<DynamicWorkAuditdata> list) throws UnsupportedEncodingException {
        try {
            dynamicWorkAuditdataService.upload(list);
            return success("数据上传成功!");
        } catch (Exception exception) {
            System.err.println(exception.getMessage());
            return failed(null, exception.getMessage());
        }
    }
}
