package com.tofly.xrtymis.custom.controller.attachment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.xrtymis.custom.service.FileService;
import com.tofly.xrtymis.custom.utils.DateTimeUtil;
import com.tofly.xrtymis.em.FileTableTypeEnum;
import com.tofly.xrtymis.entity.File;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;

/**
 * 工单附件管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/gdAttachment")
@Api(tags="工单附件管理")
public class GdAttachmentManagementController {

    private final FileService fileService;

    @GetMapping("/getGdfiles")
    @ApiOperation(value = "获取工单文件列表")
    public ResultRespone getAttachment(Page page,String fileName,String startTime,String endTime) {
        ResultRespone rest = new ResultRespone<>();
        Date sTime = null;
        Date eTime = null;
        if(StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)){

            LocalDate localDate = DateTimeUtil.fromString2LocalDate(startTime, DateTimeUtil.FULL_FORMAT);
            LocalDate localDate1 = DateTimeUtil.fromString2LocalDate(endTime, DateTimeUtil.FULL_FORMAT);
            if(null!=localDate && null!= localDate1){
                sTime = DateTimeUtil.localDateToDate(localDate);
                eTime = DateTimeUtil.localDateToDate(localDate1);
            }

        }
        try {
            rest.setResult(fileService.page(page,new QueryWrapper<File>().lambda()
                    .notLike(File::getFileFormat,"amr")
                    .notLike(File::getFileFormat,"wav")
                    .like(StringUtil.isNotNull(fileName),File::getFileName,fileName)
                    .ge(StringUtil.isNotNull(sTime),File::getCreateTime, sTime)
                    .le(StringUtil.isNotNull(eTime),File::getCreateTime, eTime)
                    .eq(File::getTableName,"TF_YWPN_GDINFO_W")));
        }catch (Exception e){
            e.printStackTrace();
            rest.setCode(-1);
            rest.setMessage(e.getMessage());
        }
        return rest;
    }



    @GetMapping("/getGdAudios")
    @ApiOperation(value = "获取工单录音列表")
    public ResultRespone getGdAudios(Page page ,String fileName,String startTime,String endTime) {
        Date sTime = null;
        Date eTime = null;
        if(StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)){
            LocalDate localDate = DateTimeUtil.fromString2LocalDate(startTime, DateTimeUtil.FULL_FORMAT);
            LocalDate localDate1 = DateTimeUtil.fromString2LocalDate(endTime, DateTimeUtil.FULL_FORMAT);
            if(null!=localDate && null!= localDate1){
                sTime = DateTimeUtil.localDateToDate(localDate);
                eTime = DateTimeUtil.localDateToDate(localDate1);
            }

        }
        ResultRespone rest = new ResultRespone<>();
        try {

            rest.setResult(fileService.page(page,new QueryWrapper<File>().lambda()
                    .like(File::getFileFormat,"amr")
                    .like(File::getFileFormat,"wav")
                    .like(StringUtil.isNotNull(fileName),File::getFileName,fileName)
                    .ge(StringUtil.isNotNull(sTime),File::getCreateTime, sTime)
                    .le(StringUtil.isNotNull(eTime),File::getCreateTime, eTime)
                    .eq(File::getTableName,"TF_YWPN_GDINFO_W")));
        }catch (Exception e){
            e.printStackTrace();
            rest.setCode(-1);
            rest.setMessage(e.getMessage());
        }
        return rest;
    }

}
