package com.yx.face.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.component.security.Authority;
import com.yx.face.boot.core.BaseController;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.RestResponse;
import com.yx.face.boot.restful.RestResult;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.boot.uitls.ConvertUtils;
import com.yx.face.boot.uitls.ExcelListenerUtils;
import com.yx.face.model.dto.AdminDTO;
import com.yx.face.model.excel.AdminInfoExcel;
import com.yx.face.model.search.AdminSearch;
import com.yx.face.model.update.AdminUpdate;
import com.yx.face.model.vo.AdminVO;
import com.yx.face.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @description: 管理人员信息表(Admin)表控制层 <br>
 * @author: PWB <br>
 * @since: 1.0 <br>
 * @date: 2021-05-21 13:37:01 <br>
 */

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "B 管理人员权限相关功能API")
@RestController
@RequestMapping("admin/auth")
public class AdminController extends BaseController {

    /**
     * 服务对象
     */
    private final AdminService adminService;



    @ApiOperation(value = "登录")
    @Authority(required = false)
    @GetMapping("doLogin")
    public RestResult<AdminVO> doLogin(@RequestParam String username, @RequestParam String password) {
        return RestResponse.ok(adminService.login(username, password));
    }

    @ApiOperation(value = "退出")
    @Authority(required = false)
    @GetMapping("doLogout")
    public RestResult<Boolean> doLogout() {
        return RestResponse.ok(adminService.logout());
    }


    /**
     * @description: 获取管理员信息 <br>
     * @date: 2021/5/4 17:08 <br>
     * @author: PWB <br>
     */
    @ApiOperation(value = "获取管理员信息")
    @GetMapping("doGetInfo")
    public RestResult<AdminVO> doGetInfo() {
        return RestResponse.ok(adminService.getInfo(getUserId()));
    }

    /**
     * @param dto <br>
     * @description: 添加/编辑管理员 <br>
     * @date: 2021/5/4 17:09 <br>
     * @author: PWB <br>
     */
    @ApiOperation(value = "添加/编辑管理员")
    @Authority(value = {1,2})
    @PostMapping("doEditAdmin")
    public RestResult<Boolean> doEditAdmin(@RequestBody AdminDTO dto) {
        if (StringUtils.hasText(dto.getIdNumber())) {
            if (!ConvertUtils.idCardJudge(dto.getIdNumber())) {
                throw new ServiceException("身份证号格式不正确");
            }
        }
        if (StringUtils.hasText(dto.getPhone())) {
            if (!ConvertUtils.phoneJudge(dto.getPhone())) {
                throw new ServiceException("手机号格式不正确");
            }
        }
        if (StringUtils.hasText(dto.getPassword())) {
            if (dto.getPassword().length() < 6) {
                throw new ServiceException("密码不能少于六位");
            }
        }
        return RestResponse.ok(adminService.editAdmin(dto));
    }


    /**
     * @param dto <br>
     * @description: 获取管理员列表 <br>
     * @date: 2021/5/4 17:09 <br>
     * @author: PWB <br>
     */
    @ApiOperation(value = "获取管理员列表")
    @PostMapping("doGetAdmins")
    @Authority(value = {1,2,3,4})
    public RestResult<PageInfo<AdminVO>> doGetAdmins(@RequestBody RestDTO<AdminSearch> dto) {
        return RestResponse.ok(adminService.getAdmins(dto));
    }
    @ApiOperation(value = "获取管理员列表(id 和 username)")
    @PostMapping("getAdminlist")
    public RestResult<List<AdminVO>> getAdminlist(@RequestBody JSONObject type) {
        return RestResponse.ok(adminService.getAdminlist(type));
    }

    /**
     * @param id <br>
     * @description: 删除管理员 <br>
     * @date: 2021/5/4 17:09 <br>
     * @author: PWB <br>
     */
    @ApiOperation(value = "删除管理员")
    @Authority(value = {1,2})
    @DeleteMapping("doDeleteAdmin")
    public RestResult<Boolean> doDeleteAdmin(@RequestParam Long id) {
        return RestResponse.ok(adminService.deleteAdmin(id));
    }

    @ApiOperation("管理员的批量导入")
    @PostMapping("uploadAdminInfoExcel")
    public RestResult<List<String>> uploadAdminInfoExcel(@RequestParam("file") MultipartFile excelFile) {
        ExcelListenerUtils listener = new ExcelListenerUtils();  //ExcelListener
        try {
            EasyExcel.read(excelFile.getInputStream(), AdminInfoExcel.class,listener).sheet(0).doReadSync();  //ExcelModel 上面创建的实体类
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> datas = listener.getDatas(); //把拿到的数据处理到集合里去(根据所需自行修改)
        return adminService.uploadAdminInfoExcel(datas);
    }
    @ApiOperation(value = "管理员修改自己的信息")
    @PostMapping("updateAdmin")
    public RestResult<Boolean> updateAdmin(@RequestBody AdminUpdate adminUpdate) {
        return RestResponse.ok(adminService.updateAdmin(adminUpdate));
    }
}

