package com.jojubanking.boot.module.system.controller.admin.xyrw;

import com.jojubanking.boot.module.system.controller.app.xytjinfo.vo.XyrwListReqVO;
import com.jojubanking.boot.module.system.dal.dataobject.student.StudentDO;
import com.jojubanking.boot.module.system.service.student.StudentService;
import com.jojubanking.boot.module.system.util.xytj.DateDUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.xyrw.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.xyrw.XyrwDO;
import com.jojubanking.boot.module.system.convert.xyrw.XyrwConvert;
import com.jojubanking.boot.module.system.service.xyrw.XyrwService;

@Api(tags = "管理后台 - 批次信息")
@RestController
@RequestMapping("/system/xyrw")
@Validated
public class XyrwController {

    @Resource
    private XyrwService xyrwService;
    @Resource
    private StudentService studentService;

    @PostMapping("/create")
    @ApiOperation("创建批次信息")
    @PreAuthorize("@ss.hasPermission('system:xyrw:create')")
    public CommonResult<Long> createXyrw(@Valid @RequestBody XyrwCreateReqVO createReqVO) {
        //新建批次
        //批次号命名规则：XYTJ20240612001，XYTJ20240618002........;
        //XYTJ+年月日+顺序号排下去，到了2025年，再从001、002开始，如XYTJ20250318001，XYTJ20250915002；
        //1.获取上一个批次号，如果为空，则从001开始
        //2.非空，比较是否跨年
        //获取下园批次，只取开始状态的批次
        String curBatchno = "";
        XyrwListReqVO xyrwListReqVO = new XyrwListReqVO();
        xyrwListReqVO.setBatchStatus(1);
        List<XyrwDO> listXyrw = xyrwService.getXyrwSimpleList(xyrwListReqVO);
        if(listXyrw.size() > 0){
            curBatchno = listXyrw.get(0).getBatchno();
            String bno = curBatchno.substring(12);
            String bdate = curBatchno.substring(4, 8);
            if(bdate.equals(DateDUtil.getCurrentDate().substring(0,4))){
                Long lno = Long.parseLong(bno);
                lno = lno + 1;
                if(lno == 999L ) {
                    lno = 1L;
                }
                curBatchno = "XYTJ" + DateDUtil.getCurrentDate() + String.format("%03d", lno);
            }else{
                curBatchno = "XYTJ" + DateDUtil.getCurrentDate() + "001";
            }
        }else{
            curBatchno = "XYTJ" + DateDUtil.getCurrentDate() + "001";
        }
        createReqVO.setBatchno(curBatchno);

        long id = xyrwService.createXyrw(createReqVO);
        //更新其他批次为结束
        XyrwUpdateReqVO updateReqVO = new XyrwUpdateReqVO();
        updateReqVO.setBatchStatus(2);
        //updateReqVO.setBatchno(createReqVO.getBatchno());
        updateReqVO.setBatchno(curBatchno);
        xyrwService.updateXyrwByBatch(updateReqVO);

        return success(id);
    }

    @PutMapping("/update")
    @ApiOperation("更新批次信息")
    @PreAuthorize("@ss.hasPermission('system:xyrw:update')")
    public CommonResult<Boolean> updateXyrw(@Valid @RequestBody XyrwUpdateReqVO updateReqVO) {
        xyrwService.updateXyrw(updateReqVO);

        //修改对应学生表里面的批次名称
        StudentDO studentDO = new StudentDO();
        studentDO.setBatchname(updateReqVO.getBatchName());

        studentDO.setBatchno(updateReqVO.getBatchno());
        studentService.updateStudentXyrwName(studentDO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除批次信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:xyrw:delete')")
    public CommonResult<Boolean> deleteXyrw(@RequestParam("id") Long id) {
        xyrwService.deleteXyrw(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得批次信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:xyrw:query')")
    public CommonResult<XyrwRespVO> getXyrw(@RequestParam("id") Long id) {
        XyrwDO xyrw = xyrwService.getXyrw(id);
        return success(XyrwConvert.INSTANCE.convert(xyrw));
    }

    @GetMapping("/list")
    @ApiOperation("获得批次信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:xyrw:query')")
    public CommonResult<List<XyrwRespVO>> getXyrwList(@RequestParam("ids") Collection<Long> ids) {
        List<XyrwDO> list = xyrwService.getXyrwList(ids);
        return success(XyrwConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得批次信息分页")
    @PreAuthorize("@ss.hasPermission('system:xyrw:query')")
    public CommonResult<PageResult<XyrwRespVO>> getXyrwPage(@Valid XyrwPageReqVO pageVO) {
        PageResult<XyrwDO> pageResult = xyrwService.getXyrwPage(pageVO);
        return success(XyrwConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出批次信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:xyrw:export')")
    @OperateLog(type = EXPORT)
    public void exportXyrwExcel(@Valid XyrwExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<XyrwDO> list = xyrwService.getXyrwList(exportReqVO);
        // 导出 Excel
        List<XyrwExcelVO> datas = XyrwConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "批次信息.xls", "数据", XyrwExcelVO.class, datas);
    }

}
