package com.chushang.inspection.terminal.controller;

import cn.hutool.core.bean.BeanUtil;
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.project.dto.Delete;
import com.chushang.inspection.project.po.PollingTaskAutograph;
import com.chushang.inspection.terminal.po.Terminal;
import com.chushang.inspection.terminal.po.TerminalIns;
import com.chushang.inspection.terminal.query.TerminalAppQuery;
import com.chushang.inspection.terminal.query.TerminalQuery;
import com.chushang.inspection.terminal.service.TerminalService;
import com.chushang.security.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
* 终端信息表(st_terminal)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/terminal")
public class TerminalController {

    @Resource
    TerminalService terminalService;

    /**
     * // 之前在这里是派单
     * 商户终端池
     * @param query 条件
     */
    @SysLog(value = "终端商户池", businessType = BusinessType.QUERY)
    @GetMapping("/page")
    @RequiresPermissions("ins:terminal:page")
    public AjaxResult pageList(@Validated TerminalQuery query) {
        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(terminalService.pageList(query));
    }


    /**
     * 查询工单详情
     */
    @SysLog("派单记录")
    @GetMapping("/dispatchRecord")
    @RequiresPermissions("ins:terminal:record")
    public AjaxResult dispatchRecord(@PathVariable TerminalQuery query)
    {
        return AjaxResult.success(terminalService.dispatchRecord(query));
    }

    /**
     * 查询终端app
     * @param query 条件
     */
    @SysLog(value = "终端app", businessType = BusinessType.QUERY)
    @GetMapping("/page/app")
    @RequiresPermissions("terminal:page:app")
    public AjaxResult queryPageApp(@Validated TerminalAppQuery query) {
        return AjaxResult.success(terminalService.queryPageApp(query));
    }

    /**
     * 查询终端
     */
    @SysLog(value = "终端详情", businessType = BusinessType.QUERY)
    @GetMapping("/info/{terminalId}")
    @RequiresPermissions("ins:terminal:info")
    public AjaxResult info(@PathVariable Long terminalId) {
        return AjaxResult.success(terminalService.getById(terminalId));
    }

    /**
     * 查询终端app
     */
    @SysLog(value = "终端详情app", businessType = BusinessType.QUERY)
    @GetMapping("/info/app/{terminalId}")
    @RequiresPermissions("terminal:info:app")
    public AjaxResult infoApp(@PathVariable Long terminalId,
                              @RequestParam("taskId") Long taskId) {
        return AjaxResult.success(terminalService.getAppById(terminalId, taskId));
    }

    /**
     * 修改 -内改终端
     * @param query 条件
     */
    @SysLog(value = "修改终端", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @RequiresPermissions("ins:terminal:update")
    public AjaxResult update(@RequestBody @Validated TerminalQuery query) {
        return AjaxResult.success(terminalService.updateTerminalAndStore(query));
    }

    @DeleteMapping(value = "/del/{terminalId}")
    @SysLog(value = "终端", businessType = BusinessType.DELETE)
    @RequiresPermissions("ins:terminal:delete")
    public AjaxResult delTerminal(@PathVariable Long terminalId){
        return AjaxResult.success(terminalService.removeById(terminalId));
    }

    @PostMapping(value = "/del/batch")
    @SysLog(value = "批量终端", businessType = BusinessType.DELETE)
    @RequiresPermissions("ins:terminal:delete")
    public AjaxResult delTerminal(@RequestBody @Validated Delete delete){
        return AjaxResult.success(terminalService.removeBatchByIds(delete.getIds()));
    }
    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;
    }
}
