package cn.edu.hactcm.cotroller;

import cn.edu.hactcm.common.R;
import cn.edu.hactcm.entity.Employee;
import cn.edu.hactcm.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> Login(@RequestBody Employee employee, HttpServletRequest request){

        //封装条件，根据用户名查询并返回对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //取出密码
        String passWord = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        //对用户名是否存在进行校验
        if (emp == null) return R.error("该账号不存在！");

        //用户存在对密码进行校验
        if (!emp.getPassword().equals(passWord)) return R.error("用户名或密码错误，请重试！");

        //对用户状态进行验证
        if (emp.getStatus() == 0) return R.error("该账号已禁用，如有疑问，请联系管理员！");

        //登录成功，将用户ID存入session
        HttpSession session = request.getSession();
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        return R.success("退出成功！");
    }


    /**
     * 添加员工
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R addEmployee(@RequestBody Employee employee,HttpServletRequest request){

        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,username);

        //：添加用户时如果用户名重复的处理方式一
        /*Employee employee1 = employeeService.getOne(queryWrapper);
        if (employee1 != null) {
            return R.error("用户名已存在！");
        }*/

        //添加用户时如果用户名重复的处理方式二：设置全局异常处理controller,对SQLIntegrityConstraintViolationException该异常进行捕获

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));*/

        boolean flag = employeeService.save(employee);

        if (flag) return R.success(null);
        return R.error("操作失败！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        pageInfo = employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 启用禁用功能
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){
        /*employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));*/
        boolean flag = employeeService.updateById(employee);
        if (flag == true) {
            return R.success("员工id修改成功");
        }
        return R.error("修改失败，请重试！");
    }

    /**
     * 修改页面回显数据功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("未知原因，请重试！");
    }



}
