package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzt.airconditionersystem.bean.Company;
import com.yzt.airconditionersystem.bean.User;
import com.yzt.airconditionersystem.service.CompanyService;
import com.yzt.airconditionersystem.vo.BasePageVo;
import com.yzt.airconditionersystem.bean.Gateway;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.GatewayService;
import com.yzt.airconditionersystem.util.BasePageVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author TzZach
 * @since 2022-04-17 11:54
 */
@Slf4j
@Controller
public class GatewayController {

    private static final String PREFIX = "gateway/";

    @Autowired
    private GatewayService gatewayService;

    @Autowired
    private CompanyService companyService;

    @GetMapping("/gateway/index")
    public String gatewayList(@RequestParam(value = "projectId", required = false) String projectId, HttpSession session) {
        session.setAttribute("projectId", projectId);
        return PREFIX + "gateway-list";
    }

    @GetMapping("/gateway/page")
    @ResponseBody
    public BasePageVo gatewayPage(@RequestParam("page") Long page,
                                  @RequestParam("limit") Long limit,
                                  HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        LambdaQueryWrapper<Gateway> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Gateway::getCompanyId, loginUser.getCompanyId());
        System.out.println(loginUser.getCompanyId());
        Page<Gateway> pageInfo = new Page<>(page, limit);
        pageInfo = gatewayService.page(pageInfo, queryWrapper);
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);
        return pageVo;
    }

    @GetMapping("/mobile/gateway/page")
    @ResponseBody
    public BasePageVo gatewayPage(@RequestParam("page") Long page,
                                  @RequestParam("limit") Long limit,
                                  @RequestParam("companyId") String companyId) {
//        User loginUser = (User) session.getAttribute("loginUser");
        LambdaQueryWrapper<Gateway> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Gateway::getCompanyId, companyId);
//        System.out.println(loginUser.getCompanyId());
        Page<Gateway> pageInfo = new Page<>(page, limit);
        pageInfo = gatewayService.page(pageInfo, queryWrapper);
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);
        return pageVo;
    }

    @GetMapping("/gateway/add")
    public String gatewayAdd() {
        return PREFIX + "gateway-add";
    }


    @GetMapping("/gateway/edit")
    public String gatewayEdit(@RequestParam("id") String id, Model model) {
        Gateway gateway = gatewayService.getById(id);
        model.addAttribute("gateway", gateway);
        return PREFIX + "gateway-edit";
    }

    @PostMapping("/gateway/add")
    @ResponseBody
    public Result addGateway(@RequestBody Gateway gateway) {
        gatewayService.save(gateway);
        return Result.success("添加成功");
    }

    @PostMapping("/gateway/delete")
    @ResponseBody
    public Result deleteGateway(@RequestBody Gateway gateway) {
        gatewayService.removeById(gateway);
        return Result.success();
    }

    @PostMapping("/gateway/edit")
    @ResponseBody
    public Result updateGateway(@RequestBody Gateway gateway) {
        gatewayService.updateById(gateway);
        return Result.success();
    }

    @PostMapping("/gateway/batchDelete")
    @ResponseBody
    public Result deleteBatchGateway(@RequestBody List<String> ids) {
        gatewayService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/gateway/detail")
    @ResponseBody
    public Result gatewayDetail(@RequestParam("id") String id) {
        Gateway gateway = gatewayService.getById(id);
        Company company = companyService.getById(gateway.getCompanyId());
        gateway.setCompany(company);
        return Result.success(gateway);
    }

}
