package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.airconditionersystem.bean.Company;
import com.yzt.airconditionersystem.bean.Operator;
import com.yzt.airconditionersystem.bean.User;
import com.yzt.airconditionersystem.enums.CompanyTypeEnum;
import com.yzt.airconditionersystem.service.CompanyService;
import com.yzt.airconditionersystem.service.UserService;
import com.yzt.airconditionersystem.vo.BasePageVo;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.OperatorService;
import com.yzt.airconditionersystem.util.BasePageVoUtil;
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 java.util.List;
import java.util.Map;


/**
 * @author TzZach
 * @since 2022-04-13 14:51
 */
@Slf4j
@Controller
public class OperatorController {

    public static final String PREFIX = "operator/";

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

    @ResponseBody
    @GetMapping(value = {"/operator/page"})
    public BasePageVo operatorListPage(@RequestParam("page") Long page,
                                       @RequestParam("limit") Long limit) {
        Page<Company> pageInfo = new Page<>(page, limit);
        pageInfo = companyService.getCompanyPageByType(pageInfo, CompanyTypeEnum.OPERATOR.getId());
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);
        return pageVo;
    }


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


    @GetMapping("/operator/edit")
    public String operatorEdit(@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 + "operator-edit";
    }

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

    @PostMapping("/operator/add")
    @ResponseBody
    public Result addOperator(@RequestBody CompanyVo company) {
        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("该公司名称已存在！");
        }
        System.out.println(company);
        company.setType(CompanyTypeEnum.OPERATOR.getId());
        companyService.save(company);
        User insertUser = new User(null, company.getUsername(), company.getPassword(),
                CompanyTypeEnum.OPERATOR.getId(), company.getId());
        userService.save(insertUser);
        return Result.success( "添加成功");
    }

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

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

    @PostMapping("/operator/batchDelete")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        companyService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/operator/search")
    @ResponseBody
    public BasePageVo operatorSearch(@RequestParam("page") Long page,
                                     @RequestParam("limit") Long limit,
                                     Company operator) {
        String name = operator.getName();
        String district = operator.getDistrict();
        String contact = operator.getContact();

        PageHelper.startPage(Math.toIntExact(page), Math.toIntExact(limit));
        List<Company> companies = companyService.listByNameAndDistrictAndContact(name, district, contact, CompanyTypeEnum.OPERATOR.getId());
        int navigatePages = (int) (companies.size() / limit);
        PageInfo<Company> pageInfo = new PageInfo<>(companies, navigatePages);
        BasePageVo pageVo = BasePageVoUtil.getPageVo(pageInfo);

        return pageVo;
    }

    @GetMapping("/operator/detail")
    @ResponseBody
    public Result getOperatorById(@RequestParam("id") String id) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getId, id)
                        .eq(Company::getType, CompanyTypeEnum.OPERATOR.getId());
        Company company = companyService.getOne(queryWrapper);

        return Result.success(company);
    }

}
