package com.gci.boot.modules.customer.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.customer.entity.TbScheduledPlan;
import com.gci.boot.modules.customer.service.ITbScheduledPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 定时任务记录表 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/customer/scheduledPlan")
public class TbScheduledPlanController {
    @Autowired
    private ITbScheduledPlanService iTbScheduledPlanService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "定时任务记录-分页列表查询")
    public Result<?> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<TbScheduledPlan> page = new Page<TbScheduledPlan>(pageNo, pageSize);
        LambdaQueryWrapper<TbScheduledPlan> lambdaQueryWrapper = new LambdaQueryWrapper<TbScheduledPlan>();
        IPage<TbScheduledPlan> pageList = iTbScheduledPlanService.page(page, lambdaQueryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加巡检任务
     */
    @AddSysLog(value = "定时任务记录-添加巡检任务")
    @PostMapping(value = "/addInspection")
    public Result<?> addInspection(@RequestBody(required = false) @Validated(SaveValidated.class) List<TbScheduledPlan> list) {
        if (ObjectUtil.isNull(list)) {
            return Result.error("缺少必要参数！");
        }
        iTbScheduledPlanService.addInspection(list);
        return Result.ok("保存成功！");
    }

    /**
     * 查询巡检任务
     */
    @AddSysLog(value = "定时任务记录-查询巡检任务")
    @GetMapping(value = "/getInspectionList")
    @DataPermission(item = "tb_scheduled_plan.projectId",sqlIds = {"com.gci.boot.modules.customer.mapper.TbScheduledPlanMapper.selectList"})
    public Result<?> getInspectionList() {
        LambdaQueryWrapper<TbScheduledPlan> eq = new LambdaQueryWrapper<TbScheduledPlan>().eq(TbScheduledPlan::getPlanType, "2");
        return Result.ok(iTbScheduledPlanService.list(eq));
    }

    /**
     * 查询巡检任务
     */
    @AddSysLog(value = "定时任务记录-查询巡检任务")
    @GetMapping(value = "/getCustomerList")
    @DataPermission(item = "tb_scheduled_plan.projectId",sqlIds = {"com.gci.boot.modules.customer.mapper.TbScheduledPlanMapper.selectList"})
    public Result<?> getCustomerList() {
        LambdaQueryWrapper<TbScheduledPlan> eq = new LambdaQueryWrapper<TbScheduledPlan>().eq(TbScheduledPlan::getPlanType, "1");
        return Result.ok(iTbScheduledPlanService.list(eq));
    }

    /**
     * 添加客户回访
     */
    @AddSysLog(value = "定时任务记录-添加客户回访")
    @PostMapping(value = "/addCustomer")
    public Result<?> addCustomer(@RequestBody(required = false) @Validated(SaveValidated.class) List<TbScheduledPlan> list) {
        if (ObjectUtil.isEmpty(list)) {
            return Result.error("缺少必要参数！");
        }
        iTbScheduledPlanService.addCustomer(list);
        return Result.ok("保存成功！");
    }

    @GetMapping(value = "/init")
    public Result<?> init (@RequestParam String time) {
        iTbScheduledPlanService.initPlan(time);
        return Result.ok();
    }
}
