package com.hchyun.test.controller;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;


import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.controller.HcyBaseController;
import com.hchyun.common.utils.ServerResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hchyun.common.annotation.Log;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.enums.BusinessType;
import com.hchyun.test.entity.Stu;
import com.hchyun.test.service.StuService;
import com.hchyun.common.utils.poi.ExcelUtil;
import com.hchyun.common.core.page.TableDataInfo;

/**
 * 学生Controller
 *
 * @author hchyun
 * @date 2021-01-23
 */
@RestController
@RequestMapping("/test/stu")
public class StuController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(StuController.class);

    @Autowired
    private StuService stuService;

    /**
     * 查询学生列表
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:list')")
    @GetMapping("/list")
    public Serializable list(Stu stu) {
        try {
            startPage();
            ServerResult<List<Stu>> serverResult = stuService.selectStuList(stu);
            if (serverResult.isStart()) {
                return getDataTable(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出学生列表
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Stu stu) {
        try {
            ServerResult<List<Stu>> serverResult = stuService.selectStuList(stu);
            ExcelUtil<Stu> util = new ExcelUtil<Stu>(Stu.class);
            if (serverResult.isStart()) {
                return util.exportExcel(serverResult.getData(), "stu");
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<Stu> serverResult = stuService.selectStuById(id);
            if (serverResult.isStart()) {
                return AjaxResult.success(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stu stu) {
        if (stu.getName() == null || stu.getName().equals("")) {
            return AjaxResult.error("学生姓名不能为空!");
        }
        if (stu.getTel() == null || stu.getTel() < 0) {
            return AjaxResult.error("电话不能为空!");
        }
        if (!Pattern.matches("^1[0-9]{10}$", String.valueOf(stu.getTel()))) {
            return AjaxResult.error("电话格式错误!");
        }
        if (stu.getEmail() == null || stu.getEmail().equals("")) {
            return AjaxResult.error("电子邮件不能为空!");
        }
        if (!Pattern.matches("^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", stu.getEmail())) {
            return AjaxResult.error("电子邮件格式错误!");
        }
        try {
            ServerResult<Integer> serverResult = stuService.insertStu(stu);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stu stu) {
        try {

            if (stu.getName() == null || stu.getName().equals("")) {
                return AjaxResult.error("学生姓名不能为空!");
            }
            if (stu.getTel() == null || stu.getTel() < 0) {
                return AjaxResult.error("电话不能为空!");
            }
            if (!Pattern.matches("^1[0-9]{10}$", String.valueOf(stu.getTel()))) {
                return AjaxResult.error("电话格式错误!");
            }
            if (stu.getEmail() == null || stu.getEmail().equals("")) {
                return AjaxResult.error("电子邮件不能为空!");
            }
            if (!Pattern.matches("^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", stu.getEmail())) {
                return AjaxResult.error("电子邮件格式错误!");
            }
            ServerResult<Integer> serverResult = stuService.updateStu(stu);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@hchyun.hasPermi('test:stu:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            if (ids.length < 0) {
                return AjaxResult.error("id不能为空!");
            }
            ServerResult<Integer> serverResult = stuService.deleteStuByIds(ids);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
