package com.chushang.inspection.work.controller;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.inspection.terminal.query.TerminalQuery;
import com.chushang.inspection.work.service.WrkInfoTerminalRecordService;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* 工单终端信息(wrk_info_terminal_record)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/wrk/info/terminal")
public class WrkInfoTerminalRecordController {

    /**
     * 我的终端池
     */
    @Resource
    private WrkInfoTerminalRecordService terminalRecordService;

    /**
     * 我的终端池
     * @param query 条件
     */
    @SysLog(value = "我的终端池", businessType = BusinessType.QUERY)
    @GetMapping("/mine/page")
    @RequiresPermissions("ins:terminal:mine")
    public AjaxResult minePageList(@Validated TerminalQuery query) {
        // 获取当前用户的
        query.setUserId(SecurityUtils.getUserId());
        List<String> createTimes = query.getCreateTimes();
        List<String> inspectionTime =   query.getInspectionTime();
        query.setCreateTimes(extendTimeRange(createTimes, " 00:00:00", " 23:59:59"));
        query.setInspectionTime(extendTimeRange(inspectionTime, " 00:00:00", " 23:59:59"));
        return AjaxResult.success(terminalRecordService.minePageList(query));
    }
    private List<String> extendTimeRange(List<String> timeList, String prefix, String suffix) {
        if (timeList != null && timeList.size() >= 2) {
            String startTime = timeList.get(0) + prefix;
            String endTime = timeList.get(1) + suffix;
            timeList.set(0, startTime);
            timeList.set(1, endTime);
        }
        return timeList;
    }
}
