package com.yzt.airconditionersystem.controller;

import com.azul.crs.client.service.ClientService;
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.enums.CompanyTypeEnum;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.CompanyService;
import com.yzt.airconditionersystem.service.UserService;
import com.yzt.airconditionersystem.util.BasePageVoUtil;
import com.yzt.airconditionersystem.vo.BasePageVo;
import com.yzt.airconditionersystem.vo.CompanyVo;
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-13 14:51
 */
@Slf4j
@Controller
public class ClientController {

    public static final String PREFIX = "client/";

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping(value = {"/client/page"})
    public BasePageVo clientListPage(@RequestParam("page") Long page,
                                     @RequestParam("limit") Long limit,
                                     HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        String companyId = loginUser.getCompanyId();
        Page<Company> pageInfo = new Page<>(page, limit);
        pageInfo = companyService.getCompanyPageByType(pageInfo, CompanyTypeEnum.CLIENT.getId(), companyId);
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);
        return pageVo;
    }


    @GetMapping( "/client/index")
    public String clientList(Model model) {
        return PREFIX + "client-list";
    }


    @GetMapping("/client/edit")
    public String clientEdit(@RequestParam("id") String id, Model model) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Company::getId, id);
        Company company = companyService.getOne(queryWrapper);
        model.addAttribute("company", company);
        return PREFIX + "client-edit";
    }

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

    @PostMapping("/client/add")
    @ResponseBody
    public Result addClient(@RequestBody CompanyVo company,
                            HttpSession session) {
        User user = userService.getOneByUsername(company.getUsername());
        if (user != null) {
            return Result.error("该用户名已存在！");
        }
        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyLambdaQueryWrapper.eq(Company::getName, company.getName());
        if (companyService.getOne(companyLambdaQueryWrapper) != null) {
            return Result.error("该公司名称已存在！");
        }
        User loginUser = (User) session.getAttribute("loginUser");
        String companyId = loginUser.getCompanyId();
        company.setType(CompanyTypeEnum.CLIENT.getId());
        company.setOperatorId(companyId);
        companyService.save(company);
        User insertUser = new User(null, company.getUsername(), company.getPassword(),
                CompanyTypeEnum.CLIENT.getId(), company.getId());
        userService.save(insertUser);
        return Result.success( "添加成功");
    }

    @PostMapping("/client/edit")
    @ResponseBody
    public Result updateClient(@RequestBody Company company) {
        companyService.updateById(company);
        return Result.success( "修改成功");
    }

    @PostMapping("/client/delete")
    @ResponseBody
    public Result deleteClient(@RequestBody Company company) {
        companyService.removeById(company.getId());
        return Result.success( "删除成功");
    }

    @PostMapping("/client/batchDelete")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        System.out.println(ids);
        companyService.removeBatchByIds(ids);
        return Result.success();
    }



}
