package com.xinyilu.web.controller.classinfo;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.system.domain.dto.ClassInfoDTO;
import com.xinyilu.system.domain.dto.ClassTypeDTO;
import com.xinyilu.system.service.classinfo.IClassInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 班级信息Controller
 *
 * @author jihan
 * @date 2022-07-29
 */
@RestController
@RequestMapping("/system/classInfo")
public class ClassInfoController extends BaseController {

    @Autowired
    private IClassInfoService classInfoService;

    /**
     * 查询班级信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:classinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClassInfoDTO classInfo) {
        return classInfoService.selectClassInfoList(classInfo);
    }

    /**
     * 获取班级信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(classInfoService.selectClassInfoById(id));
    }

    /**
     * 新增班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:classinfo:add')")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody ClassInfoDTO classInfo) {
        if (StringUtils.isBlank(classInfo.getEntranceDateStr())) {
            return AjaxResult.success("开学日期不能为空");
        }
        //创建人
        classInfo.setCreateBy(getUsername());
        //开学日期
        classInfo.setEntranceDate(LocalDate.parse(classInfo.getEntranceDateStr(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return toAjax(classInfoService.insertClassInfo(classInfo));
    }

    /**
     * 修改班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:classinfo:edit')")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody ClassInfoDTO classInfo) {
        if (StringUtils.isBlank(classInfo.getEntranceDateStr())) {
            return AjaxResult.success("开学日期不能为空");
        }
        //修改人
        classInfo.setUpdateBy(getUsername());
        //开学日期
        classInfo.setEntranceDate(LocalDate.parse(classInfo.getEntranceDateStr(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return toAjax(classInfoService.updateClassInfo(classInfo));
    }

    /**
     * 删除班级信息
     */
    @PreAuthorize("@ss.hasPermi('system:classinfo:remove')")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(classInfoService.updateClassInfoById(ids));
    }

    /**
     * 批量修改“班级类型”
     */
    @PreAuthorize("@ss.hasPermi('system:classinfo:updateClassType')")
    @PostMapping("/updateClassType")
    public AjaxResult updateClassType(@RequestBody ClassTypeDTO classTypeDTO) {
        if (Objects.isNull(classTypeDTO.getKindergartenId())) {
            return AjaxResult.error("幼儿园信息不能为空！");
        }
        if (StringUtils.isBlank(classTypeDTO.getOriginClassType())) {
            return AjaxResult.error("原班级类型不能为空！");
        }
        if (StringUtils.isBlank(classTypeDTO.getNowClassType())) {
            return AjaxResult.error("新班级类型不能为空！");
        }
        return classInfoService.updateClassType(classTypeDTO);
    }
}
