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.enums.CompanyTypeEnum;
import com.yzt.airconditionersystem.service.CompanyService;
import com.yzt.airconditionersystem.vo.BasePageVo;
import com.yzt.airconditionersystem.bean.Operator;
import com.yzt.airconditionersystem.bean.Project;
import com.yzt.airconditionersystem.vo.CompanyVo;
import com.yzt.airconditionersystem.vo.ProjectSearchVo;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.OperatorService;
import com.yzt.airconditionersystem.service.ProjectService;
import com.yzt.airconditionersystem.service.impl.ProjectServiceImpl;
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 java.util.*;

/**
 * @author TzZach
 * @since 2022-04-15 12:00
 */
@Controller
@Slf4j
public class ProjectController {

    private static final String PREFIX = "project/";

    @Autowired
    private CompanyService companyService;


    @GetMapping("/project-list")
    public String projectList() {
        return PREFIX + "project-list";
    }

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

    @GetMapping("/project-edit")
    public String projectEdit(@RequestParam("id") String id,
                              Model model) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getType, CompanyTypeEnum.CLIENT.getId())
                .eq(Company::getId, id);
        Company company = companyService.getOne(wrapper);
        model.addAttribute("company", company);
        return PREFIX + "project-edit";
    }

    @GetMapping("/project/page")
    @ResponseBody
    public BasePageVo projectPage(@RequestParam("page") Long page,
                                  @RequestParam("limit") Long limit) {
        Page<Company> pageInfo = new Page<>(page, limit);
        pageInfo = companyService.getCompanyPageByType(pageInfo, CompanyTypeEnum.CLIENT.getId());
        List<Company> records = pageInfo.getRecords();
        for (Company record : records) {
            record.setOperatorName(companyService.getById(record.getOperatorId()).getName());
        }
        return BasePageVoUtil.getPageVo(pageInfo);
    }

    @PostMapping("/project/delete")
    @ResponseBody
    public Result deleteProject(@RequestBody Company company) {
        companyService.removeById(company);
        return Result.success();
    }

    @PostMapping("/project/add")
    @ResponseBody
    public Result addProject(@RequestBody Company company) {
        companyService.save(company);
        return Result.success();
    }

    @PostMapping("/project/batchDelete")
    @ResponseBody
    public Result deleteProjectsByIds(@RequestBody List<String> ids) {
        companyService.removeBatchByIds(ids);
        return Result.success();
    }


    @GetMapping("/project/search")
    @ResponseBody
    public BasePageVo searchProjects(@RequestParam("page") Long page,
                                 @RequestParam("limit") Long limit,
                                 CompanyVo projectSearchVo) {

        String clientName = projectSearchVo.getClientName();
        String district = projectSearchVo.getDistrict();
        String operatorName = projectSearchVo.getOperatorName();

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

        log.info(companies.toString());

        return pageVo;
    }

    @GetMapping("/project/detail")
    @ResponseBody
    public Result projectDetail(@RequestParam("id") String id) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getId, id)
                .eq(Company::getType, CompanyTypeEnum.CLIENT.getId());
        Company client = companyService.getOne(queryWrapper);
        Company operator = companyService.getById(client.getOperatorId());
        client.setOperator(operator);
        return Result.success(client);
    }


}
