package me.zhengjie.modules.thirdparty.v1;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.base.BaseResponse;
import me.zhengjie.base.QueryPageParams;
import me.zhengjie.modules.dm.device.service.DmDeviceService;
import me.zhengjie.modules.dm.printingApproval.domain.DmPrintingApproval;
import me.zhengjie.modules.dm.printingApproval.service.DmPrintingApprovalService;
import me.zhengjie.modules.dm.printingQueue.domain.DmPrintingQueue;
import me.zhengjie.modules.dm.printingQueue.service.DmPrintingQueueService;
import me.zhengjie.modules.dm.printingRequest.domain.DmPrintingRequest;
import me.zhengjie.modules.dm.printingRequest.service.DmPrintingRequestService;
import me.zhengjie.modules.dm.printingRequest.service.dto.DmPrintingRequestDto;
import me.zhengjie.modules.dm.printingRequest.service.dto.DmPrintingRequestQueryCriteria;
import me.zhengjie.modules.dm.printingRequest.service.dto.FindDmPrintingRequestParam;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "对外接口")
@RequestMapping("/api/thirdparty/v1/open")
public class UserApiController {

    private final DmPrintingQueueService dmPrintingQueueService;
    private final DmPrintingRequestService dmPrintingRequestService;
    private final DmPrintingApprovalService dmPrintingApprovalService;
    private final DmDeviceService dmDeviceService;

    @Log("获取我的文印-App")
    @ApiOperation("获取我的文印-App")
    @AnonymousPostMapping(value = "/findByRequestUser")
    public BaseResponse<Object> findByRequestUser(@RequestBody QueryPageParams<DmPrintingRequestQueryCriteria> criteria, Pageable pageable) {
        SecurityUtils.CheckApiAuth(criteria);
        return new BaseResponse<>(dmPrintingRequestService.findByRequestUser(criteria.getQuery().getStatus(), criteria.getQuery().getPrintingRequest(), criteria.getQuery().getRequestUser(), pageable));
    }

    @Log("获取我的审批-App")
    @ApiOperation("获取我的审批-App")
    @AnonymousPostMapping(value = "/findByApp")
    public BaseResponse<Object> findByApp(@RequestBody QueryPageParams<DmPrintingRequestQueryCriteria> criteria, Pageable pageable) {
        SecurityUtils.CheckApiAuth(criteria);
        return new BaseResponse<>(dmPrintingRequestService.queryAll(criteria.getQuery(), pageable));
    }

    @Log("我的文印查询")
    @ApiOperation("我的文印查询")
    @AnonymousPostMapping(value = "/findMyWy")
    public BaseResponse<Object> findMyWy(@RequestBody QueryPageParams<FindDmPrintingRequestParam> criteria, Pageable pageable) {
        SecurityUtils.CheckApiAuth(criteria);
        return new BaseResponse<>(dmPrintingRequestService.findMyWy(criteria.getQuery(), pageable));
    }

    @Log("预约审批")
    @ApiOperation("预约审批")
    @AnonymousPostMapping("/updateApproveStatusByTaskId")
    public BaseResponse<Object> updateApproveStatusByTaskId(@RequestBody QueryPageParams<DmPrintingApproval> params) throws IOException {
        SecurityUtils.CheckApiAuth(params);
        String taskId = params.getQuery().getTaskId();
        System.out.println("taskId:" + taskId);
        String approveStatus = params.getQuery().getApprovalStatus();
        System.out.println("approveStatus" + approveStatus);
        String remark = params.getQuery().getApprovalRemark();
        System.out.println("remark:" + remark);
        String sendContent = "";
        //查询会议预约信息
        DmPrintingRequestQueryCriteria criteria = new DmPrintingRequestQueryCriteria();
        criteria.setTaskId(taskId);
        DmPrintingRequestDto dmPrintingRequest = dmPrintingRequestService.queryAll(criteria).get(0);
        if (!ObjectUtils.isEmpty(dmPrintingRequest)) {
            if (StringUtils.equals(approveStatus, "01")) {//01、已通过
                sendContent = "文印审批通过！  设备名称:" + dmPrintingRequest.getDevice().getName();
                //修改文印申请信息
                DmPrintingRequest request = new DmPrintingRequest();
                request.setId(dmPrintingRequest.getId());
                request.setStatus(approveStatus);
                request.setApprovalRemark(remark);
                dmPrintingRequestService.update(request);
                //关联DmPrintingRequest
                request.setId(Long.valueOf(taskId));
                //保存审批信息
                DmPrintingApproval dmPrintingApproval = new DmPrintingApproval();
                dmPrintingApproval.setPrintingRequest(request);
                dmPrintingApproval.setTaskCode(taskId);
                dmPrintingApproval.setTaskId(taskId);
                dmPrintingApproval.setApprovalStatus(approveStatus);
                dmPrintingApproval.setApprovalRemark(remark);
                dmPrintingApprovalService.create(dmPrintingApproval);
                //推送打印
                Timestamp timestamp = new Timestamp(System.currentTimeMillis());
                DmPrintingQueue printingQueue = new DmPrintingQueue();
                printingQueue.setPrintingRequest(request);
                printingQueue.setStatus("00");
                printingQueue.setStartTime(timestamp);
                dmPrintingQueueService.create(printingQueue);

            } else if (StringUtils.equals(approveStatus, "02")) {//02、未通过
                sendContent = "文印审批未通过！  审批者意见:" + remark;
                //修改文印申请信息
                DmPrintingRequest request = new DmPrintingRequest();
                request.setId(dmPrintingRequest.getId());
                request.setStatus(approveStatus);
                request.setApprovalRemark(remark);
                dmPrintingRequestService.update(request);
                //关联DmPrintingRequest
                request.setId(Long.valueOf(taskId));
                //保存审批信息
                DmPrintingApproval dmPrintingApproval = new DmPrintingApproval();
                dmPrintingApproval.setPrintingRequest(request);
                dmPrintingApproval.setTaskCode(taskId);
                dmPrintingApproval.setTaskId(taskId);
                dmPrintingApproval.setApprovalStatus(approveStatus);
                dmPrintingApproval.setApprovalRemark(remark);
                dmPrintingApprovalService.create(dmPrintingApproval);
                //推送打印
                Timestamp timestamp = new Timestamp(System.currentTimeMillis());
                DmPrintingQueue printingQueue = new DmPrintingQueue();
                printingQueue.setPrintingRequest(request);
                printingQueue.setStatus("00");
                printingQueue.setStartTime(timestamp);
                dmPrintingQueueService.create(printingQueue);
            }
        } else {
            return new BaseResponse<>("未查询到打印数据!", -1, "未查询到打印数据!");
        }
        dmPrintingRequestService.message(dmPrintingRequest, taskId, sendContent);
//        dmMeetingApproveService.updateApproveStatusByTaskId(params.getQuery());
        return new BaseResponse<>(true);
    }

    @Log("打印设备列表(运营)")
    @ApiOperation("打印设备列表(运营)")
    @AnonymousPostMapping(value = "/deviceList")
    public BaseResponse<Object> deviceList(@RequestBody QueryPageParams<String> param){
        SecurityUtils.CheckApiAuth(param);
        List<Map<String,Object>> deviceList = dmDeviceService.deviceList();
        return new BaseResponse<>(deviceList);
    }

    @Log("打印设备详情(运营)")
    @ApiOperation("打印设备详情(运营)")
    @AnonymousPostMapping(value = "/deviceDetail")
    public BaseResponse<Object> deviceDetail(@RequestBody QueryPageParams<String> param){
        SecurityUtils.CheckApiAuth(param);
        Map<String,Object> device = dmDeviceService.deviceDetail(Long.valueOf(param.getQuery()));
        Map<String,Object> newDevice = new HashMap<>(device);
        List<Map<String,Object>> requestList = dmPrintingRequestService.requestListByDeviceId(Long.valueOf(param.getQuery()));
        List<Map<String,Object>> newRequestList = new ArrayList<>(requestList);
        newDevice.put("requestList",newRequestList);
        return new BaseResponse<>(newDevice);
    }
    @AnonymousGetMapping(value = "firstDevice")
    @Log("首页设备信息")
    @ApiOperation("首页设备信息")
    public ResponseEntity<Object> firstDevice() {

        Map<String,Object> map = new HashMap<>();

        /**
         * 查询设备数量
         */
        Long countSum = this.dmDeviceService.findDeviceCount();

        /**
         * 查询设备数量
         */
        Long countRequest = this.dmPrintingRequestService.findRequestCount();

        map.put("countSum",countSum);
        map.put("countRequest",countRequest);
        map.put("countYun",countRequest);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @AnonymousGetMapping(value = "requestFile")
    @Log("查询打印文件")
    @ApiOperation("查询打印文件")
    public ResponseEntity<Object> requestFile() {
        List<Map<String,Object>> dmPrintingRequest =  this.dmPrintingRequestService.findRequestInfo();
        return new ResponseEntity<>(dmPrintingRequest, HttpStatus.OK);
    }

}
