package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yzt.airconditionersystem.bean.Company;
import com.yzt.airconditionersystem.bean.Role;
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.RoleService;
import com.yzt.airconditionersystem.service.UserService;
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.HashMap;
import java.util.Map;

/**
 * @author TzZach
 * @since 2022-05-11 23:03
 */
@Controller
public class UserController {

    private static final String PREFIX = "user/";

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

    @GetMapping("/user/info")
    public String userInfo(HttpSession session, Model model) {
        User loginUser = (User) session.getAttribute("loginUser");
        String companyId = loginUser.getCompanyId();
        Company company = companyService.getById(companyId);
        fillUserWithCompanyInfo(loginUser, company);
        model.addAttribute("loginUser", loginUser);
        return PREFIX + "user-info";
    }

    private void fillUserWithCompanyInfo(User loginUser, Company company) {
        loginUser.setContact(company.getContact());
        loginUser.setPhone(company.getPhone());
        loginUser.setDistrict(company.getDistrict());
        loginUser.setDetailedAddress(company.getDetailedAddress());
        loginUser.setCompanyName(company.getName());
    }

    @GetMapping("/mobile/user/info")
    @ResponseBody
    public Result userInfo(@RequestParam("id") String id) {
        User user = userService.getById(id);
        Company company = companyService.getById(user.getCompanyId());
        fillUserWithCompanyInfo(user, company);
        return Result.success(user);
    }

    @PostMapping("/user/info")
    @ResponseBody
    public Result userInfoUpdate(User user) {
        userService.updateById(user);
        return Result.success();
    }

    @GetMapping("/user/password")
    public String userPassword() {
        return PREFIX + "user-password";
    }

    @PostMapping("/user/password")
    @ResponseBody
    public Result updatePassword(@RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, user.getId())
                        .eq(User::getPassword, user.getPassword());
        User getUser = userService.getOne(queryWrapper);
        if (getUser == null) {
            return Result.error("原密码错误");
        }
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setPassword(user.getNewPassword());
        userService.updateById(updateUser);
        return Result.success("修改成功");
    }

}
