package com.xinyilu.web.controller.appletstudentmanage;

import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.domain.entity.SysDictData;
import com.xinyilu.common.custominterface.Query;
import com.xinyilu.common.utils.regex.RegexUtil;
import com.xinyilu.system.domain.dto.StudentInfoDTO;
import com.xinyilu.system.service.appletstudentmanage.IAppletStudentManageService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 家长小程序学生管理
 * @Author zh
 * @Date 2023-07-31
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/studentManage")
public class AppletStudentManageController extends BaseController {

    @Resource
    private IAppletStudentManageService appletStudentManageService;
    @Resource
    private RegexUtil regexUtil;

    /**
     * 查询学生信息
     */
    @GetMapping("/getStudent")
    public AjaxResult getStudentInformation(Long studentId) {
        if (Objects.isNull(studentId)) {
            return AjaxResult.error("学生信息不能为空！");
        }
        return appletStudentManageService.getStudentInformation(studentId);
    }

    /**
     * 新增或修改学生信息
     */
    @PostMapping("/updateStudent")
    public AjaxResult updateStudentInformation(@RequestBody @Validated(Query.class) StudentInfoDTO studentInfo) {
        if (Objects.nonNull(studentInfo) && StringUtils.isNotBlank(studentInfo.getCardNumber())) {
            boolean cardNo = regexUtil.regexCardNo(studentInfo.getCardNumber());
            if (BooleanUtils.isFalse(cardNo)) {
                return AjaxResult.error("您输入的身份证号不正确，请重新输入！");
            }
        }
        return appletStudentManageService.updateStudentInformation(studentInfo);
    }

    /**
     * 查询幼儿园信息
     */
    @GetMapping("/getKindergartenList")
    public AjaxResult getKindergartenList(String kindergartenName) {
        return appletStudentManageService.getKindergartenList(kindergartenName);
    }

    /**
     * 根据幼儿园id查询班级名称
     */
    @GetMapping("/getClassInfo")
    public AjaxResult getClassInfo(Long kindergartenId, String className) {
        if (Objects.isNull(kindergartenId)) {
            return AjaxResult.error("幼儿园id不能为空！");
        }
        return appletStudentManageService.getClassInfo(kindergartenId, className);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = appletStudentManageService.selectDictDataByType(dictType);
        if (CollectionUtils.isEmpty(data)) {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }
}