package com.template.controller;


import com.template.api.RepairAdminControllerAPI;
import com.template.common.utils.AesUtils;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.eIsSuper;
import com.template.model.enumModel.eStatu;
import com.template.model.pojo.RepairAdmin;
import com.template.model.request.freezeRepairAdminRequest;
import com.template.model.request.updateRepairAdminRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.RepairAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-05
 */
@RestController
public class RepairAdminController implements RepairAdminControllerAPI {

    @Autowired
    private RepairAdminService repairAdminService;

    /**
     * 添加账号数据
     *
     * @param ra account 账号
     *           password 密码
     *           username 用户名称
     *           phone 手机号
     * @return
     */
    @Override
    public CommonResult InsertRepairAdmin(@RequestBody RepairAdmin ra, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = 0; // 帮我们自动生成id
        try {
            result = repairAdminService.insertRepairAdmin(ra);
        } catch (Exception e) {
            if (e.getCause().getMessage().contains("'repair_admin.account_un'")) {
                return CommonResult.fail("该账号已存在！");
            }
            return CommonResult.fail("系统异常，添加失败！");
        }
        return result > 0 ? CommonResult.ok() : CommonResult.fail();
    }

    /**
     * 查询账户列表数据
     *
     * @param currentPage 当前页
     * @param pageCount   一页数据条数
     * @param account     账号
     * @param phone       手机号
     * @param userName    账号名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairAdmins(@RequestParam int currentPage, @RequestParam int pageCount, String account, String phone, String userName) {
        PageUtils<RepairAdmin> result = repairAdminService.queryPageList(currentPage, pageCount, account, phone, userName);

        return CommonResult.ok(result);
    }

    /**
     * 根据ID冻结账号账号数据
     *
     * @param frar id 数据ID
     *             statu 状态
     *             正常：0；冻结：1
     * @return
     */
    @Override
    public CommonResult freezeRepairAdminById(@RequestBody freezeRepairAdminRequest frar, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        RepairAdmin data = repairAdminService.getRepairById(frar.getId());
        if (data == null) {
            return CommonResult.fail("当前数据不存在，解冻失败！");
        }
        if (data.getStatu() == frar.getStatu()) {
            String Message = frar.getStatu() == eStatu.Freeze.getValue() ? "当前处于冻结状态，请勿重复操作！" : "当前处于未冻结状态，请勿重复操作！";
            return CommonResult.fail(Message);
        }

        String success = frar.getStatu() == eStatu.Freeze.getValue() ? "冻结成功！" : "解冻成功";
        String fail = frar.getStatu() == eStatu.Freeze.getValue() ? "冻结失败！" : "解冻失败";
        RepairAdmin ra = new RepairAdmin();
        ra.setId(frar.getId());
        ra.setStatu(frar.getStatu());
        int result = repairAdminService.updateRepairAdmin(ra);
        System.out.println(result);
        return result > 0 ? CommonResult.ok(success) : CommonResult.fail(fail);
    }

    /**
     * 根据ID删除账号数据
     *
     * @param id id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairAdminById(@RequestParam String id) {
        RepairAdmin data = repairAdminService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairAdminService.deleteRepairAdminById(id);

        return result > 0 ? CommonResult.ok() : CommonResult.fail();
    }

    /**
     * 编辑账号
     *
     * @param ra id 数据ID
     *           username 账号名称
     *           phone 手机号
     *           password 密码
     * @return
     */
    @Override
    public CommonResult updateRepairAdminById(updateRepairAdminRequest ra, @RequestHeader("user_head") String userhead, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        RepairAdmin data = repairAdminService.getRepairById(ra.getId());
        if (data == null) {
            return CommonResult.fail("当前数据不存在，编辑失败！");
        }

        //只有操作管理员才能编辑他人的密码
        String userID = AesUtils.decrypt(userhead);
        if (ra.getPassword() != null && !userID.equals(ra.getId())) {
            RepairAdmin operateData = repairAdminService.getRepairById(userID);
            if(operateData == null){
                return CommonResult.fail("操作身份不合法，编辑失败！");
            }
            if(!data.getPassword().equals(AesUtils.encrypt(ra.getPassword())) && operateData.getIsSuper() != eIsSuper.Super.getValue()){
                return CommonResult.fail("只有超级管理员才能更改密码！");
            }
        }

        int result = repairAdminService.updateRepaiRadmin(ra);
        return result > 0 ? CommonResult.ok("编辑成功") : CommonResult.fail("编辑失败");
    }
}

