package com.srs.web.controller.stureg;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletResponse;

import com.srs.common.utils.DateUtils;
import com.srs.common.utils.StringUtils;
import com.srs.comprehensive.domain.Dto.StuStatusImport;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.domain.Vo.DataScreen;
import com.srs.comprehensive.service.ISrsMajorsService;
import com.srs.framework.web.domain.server.Sys;
import com.srs.stureg.domain.GxsdxySjzx009Temp2024xsxx;
import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.domain.SrsStuRegImport;
import com.srs.stureg.service.ISrsStuRegService;
import com.srs.system.service.ISysDeptService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 新生报到学生注册信息Controller
 *
 * @author srs
 * @date 2023-07-24
 */
@Api
@RestController
@RequestMapping("/system/reg")
public class SrsStuRegController extends BaseController
{
    @Autowired
    private ISrsStuRegService srsStuRegService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    ISysDeptService sysDeptService;

    @Autowired
    ISrsMajorsService srsMajorsService;

    @PreAuthorize("@ss.hasPermi('stureg:temp:index')")
    @ApiOperation("同步班级代码")
    @PostMapping("/syncClassCode")
    public AjaxResult syncClassCode(){
        return srsStuRegService.syncClassCode();
    }


    @PreAuthorize("@ss.hasPermi('stureg:temp:index')")
    @ApiOperation("列出新生临时数据")
    @GetMapping("/listXsxxTemp")
    public TableDataInfo listXsxxTemp(GxsdxySjzx009Temp2024xsxx param){
        startPage();
        List<GxsdxySjzx009Temp2024xsxx> res =  srsStuRegService.listXsxxTemp(param);
        return getDataTable(res);
    }


    @ApiOperation("统计住宿")
    @GetMapping("/countRoomBed")
    public AjaxResult countRoomBed(){
        List<Map<String,Object>> list = srsStuRegService.countRoomBed();
        return AjaxResult.success(list);
    }


    @ApiOperation("统计学院学生性别")
    @GetMapping("/countStuXb")
    public AjaxResult countStuXb(){
        List<Map<String,Object>> list = srsStuRegService.countStuXb();
        return AjaxResult.success(list);
    }


    @ApiOperation("列出最新报到的学生")
    @GetMapping("/listNewBaodao")
    public AjaxResult listNewBaodao(){
        List<Map<String,Object>> list = srsStuRegService.listNewBaodao();
        return AjaxResult.success(list);
    }


    @ApiOperation("统计学院报到人数")
    @GetMapping("/countDeptBaodao")
    public AjaxResult countDeptBaodao(){
        List<Map<String,Object>> list = srsStuRegService.countDeptBaodao();
        return AjaxResult.success(list);
    }


    @ApiOperation("统计报到人数")
    @GetMapping("/countBaodao")
    public AjaxResult countBaodao(){
        List<DataScreen> res = srsStuRegService.countBaodao();
        return AjaxResult.success(res);
    }

    @ApiOperation("统计广西区内各城市新生人数")
    @GetMapping("/countCityStu")
    public AjaxResult countCityStu(){
        List<Map<String,Object>> list = srsStuRegService.countCityStu();
        return AjaxResult.success(list);
    }

    @ApiOperation("统计各学院新生缴费人数")
    @GetMapping("/countDeptStuMoney")
    public AjaxResult countDeptStuMoney(){
        List<DataScreen> list = srsStuRegService.countDeptStuMoney();
        return AjaxResult.success(list);
    }


    @ApiOperation("统计各学院")
    @GetMapping("/countDept")
    public AjaxResult countDept(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            List<Map<String,Object>> list = srsStuRegService.countDept();
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @ApiOperation("统计生源地")
    @GetMapping("/countArea")
    public AjaxResult countArea(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            List<DataScreen> list = srsStuRegService.countArea();
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @ApiOperation("统计前十专业报道")
    @GetMapping("/countMajorTop10")
    public AjaxResult countMajorTop10(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            List<DataScreen> list = srsStuRegService.countMajorTop10();
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @ApiOperation("统计已报道")
    @GetMapping("/countNowAt")
    public AjaxResult countNowAt(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            Map<String,Object> list = srsStuRegService.countNowAt();
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @ApiOperation("统计性别")
    @GetMapping("/countGender")
    public AjaxResult countGender(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            List<DataScreen> list = srsStuRegService.countGender();
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @ApiOperation("辅导员自己的学生")
    @GetMapping("/listFdy")
    public TableDataInfo listFdy(CphSearch param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            param.setTNo(getUsername());
            startPage();
            List<SrsStuReg> list = srsStuRegService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }
    @Log(title = "新生报到学生注册信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportFdy")
    public void exportFdy(HttpServletResponse response, CphSearch param)
    {
        List<SrsStuReg> list = srsStuRegService.listView(param);
        ExcelUtil<SrsStuReg> util = new ExcelUtil<SrsStuReg>(SrsStuReg.class);
        util.exportExcel(response, list, "辅导员自己的学生注册信息数据");
    }


    //新生考生号获取个人信息
    @PostMapping("/getOwnInfo")
    public AjaxResult getOwnInfo(@RequestParam(value="ksh",required=false) String ksh){
        if(!StringUtils.isNotEmpty(ksh)){
            ksh = getUsername();
        }
        SrsStuReg stu = srsStuRegService.getOwnInfoByKsh(ksh);
        if(stu != null){
            String sfz = stu.getSFZH().substring(0,8) + "**********";
            stu.setSFZH(sfz);
            //ksh = stu.getKSH().substring(0,4)+"**********";
            ksh = stu.getKSH();
            stu.setKSH(ksh);
        }
        return AjaxResult.success(stu);
    }



    @GetMapping("/count")
    public AjaxResult count(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            return AjaxResult.success(srsStuRegService.selectCountByRegStatus());
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    /**
     * 查询新生报到学生注册信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:list')")
    @GetMapping("/list")
    public TableDataInfo list(SrsStuReg srsStuReg)
    {
        startPage();
        List<SrsStuReg> list = srsStuRegService.selectSrsStuRegList(srsStuReg);
        return getDataTable(list);
    }

    /**
     * 导出新生报到学生注册信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:export')")
    @Log(title = "新生报到学生注册信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrsStuReg srsStuReg)
    {
        List<SrsStuReg> list = srsStuRegService.selectSrsStuRegList(srsStuReg);
        for(SrsStuReg entity:list){
            switch (entity.getRegStatus()){
                case "0":
                    entity.setRegStatus("未激活");
                    break;
                case "1":
                    entity.setRegStatus("已缴费");
                    break;
                case "2":
                    entity.setRegStatus("已激活带缴费");
                    break;
                case "3":
                    entity.setRegStatus("已报到");
                    break;
                case "4":
                    entity.setRegStatus("在校");
                    break;
                default:
                    entity.setRegStatus("未激活");
                    break;
            }
            if(entity.getIsLoan() != null) {
                switch (entity.getIsLoan()) {
                    case "0":
                        entity.setIsLoan("否");
                        break;
                    case "1":
                        entity.setIsLoan("是");
                        break;
                    default:
                        entity.setIsLoan("否");
                        break;
                }
            }
            if(entity.getIsDirection() != null) {
                switch (entity.getIsDirection()) {
                    case "0":
                        entity.setIsDirection("否");
                        break;
                    case "1":
                        entity.setIsDirection("是");
                        break;
                    default:
                        entity.setIsDirection("否");
                        break;
                }
            }
        }
        ExcelUtil<SrsStuReg> util = new ExcelUtil<SrsStuReg>(SrsStuReg.class);
        util.exportExcel(response, list, "新生报到学生注册信息数据");
    }

    /**
     * 获取新生报到学生注册信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(srsStuRegService.selectSrsStuRegById(id));
    }

    /**
     * 新增新生报到学生注册信息
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:add')")
    @Log(title = "新生报到学生注册信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrsStuReg srsStuReg)
    {
        return toAjax(srsStuRegService.insertSrsStuReg(srsStuReg));
    }

    //激活和验证学生注册信息
    //@Anonymous
    @PostMapping("/verifyXX")
    public AjaxResult verifyXX(@RequestBody Map<String,Object> jsonMap){
        Map aLong = srsStuRegService.verifyXX(jsonMap);
        if (aLong.get("state")=="1"){
            return success(aLong.get("msg").toString());
        }
        return error(aLong.get("msg").toString());
    }

    @PostMapping("/verifyPhone")
    public AjaxResult verifyPhone(@RequestBody Map<String,Object> jsonMap){
        Map aLong = srsStuRegService.verifyPhone(jsonMap);
        if (aLong.get("state")=="1"){
            return success(aLong.get("msg").toString());
        }
        return error(aLong.get("msg").toString());
    }
    @PostMapping("/verifyPwd")
    public AjaxResult verifyPwd(@RequestBody Map<String,Object> jsonMap){
        Map aLong = srsStuRegService.verifyPwd(jsonMap);
        if (aLong.get("state")=="1"){
            return success(aLong.get("msg").toString());
        }
        return error(aLong.get("msg").toString());
    }
    @PostMapping("/checkPwd")
    public AjaxResult checkPwd(@RequestBody Map<String,Object> jsonMap){
        Map aLong = srsStuRegService.checkPwd(jsonMap);
        if (aLong.get("state")=="1"){
            return success(aLong.get("msg").toString());
        }
        return error(aLong.get("msg").toString());
    }
    ////完善注册信息
    //@Anonymous
    @PostMapping("/addStuRegList")
    public AjaxResult addStuRegList(@RequestBody Map<String,Object> jsonMap){
        Map aLong = srsStuRegService.insertStuRegList(jsonMap);
        if (aLong.get("state")=="1"){
            return toAjax(1).put("学号",aLong.get("msg"));
        }
        return error(aLong.get("msg").toString());
    }

    /**
     * 修改新生报到学生注册信息
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:edit')")
    @Log(title = "新生报到学生注册信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SrsStuReg srsStuReg)
    {
        return toAjax(srsStuRegService.updateSrsStuReg(srsStuReg));
    }

    /**
     * 删除新生报到学生注册信息
     */
//    @PreAuthorize("@ss.hasPermi('system:reg:remove')")
    @Log(title = "新生报到学生注册信息", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult remove(@RequestBody List<Long> ids)
    {
        return toAjax(srsStuRegService.deleteSrsStuRegByIds(ids));
    }

    //批量导入学生注册信息
    @ApiOperation("学生报道注册信息导入")
    @PostMapping("/addSrsStuRegList")
    public AjaxResult addSrsStuRegList(@RequestBody List<Map<String,Object>> srsStuRegs) throws ExecutionException, InterruptedException {
        //return toAjax(srsStuRegService.addSrsStuRegList(srsStuRegs));
        List<SrsStuReg> stringFuture = srsStuRegService.addSrsStuRegList(srsStuRegs,"");
//        int i=Integer.parseInt(stringFuture.get());
//        return success("成功导入 "+ i+"条数据");
        return success(stringFuture);
    }

    //dbf文件导入
    @ApiOperation("dbf文件信息导入")
    @PostMapping("/addDbf")
    public AjaxResult addDbf(MultipartFile file,String tableName){
        srsStuRegService.addDbf(file, tableName);
        return success("成功导入数据");
    }

    @ApiOperation("dbf文件信息导入学生注册信息")
    @PostMapping("/addDbfData")
    public AjaxResult insertDbf(MultipartFile file){
        List<Map<String, Object>> mapList = srsStuRegService.insertDBF(file);
        if(mapList!=null) {
            return success();
        }else {
            return error();
        }
    }


    /**
     *学生完善信息
     */
    @PostMapping("/stuInfo")
    public AjaxResult stuEdit(@RequestBody SrsStuReg srsStuReg)
    {
        return toAjax(srsStuRegService.updateSrsStuRegByKSH(srsStuReg));
    }
    /**
     * 查询完善信息
     */
    @GetMapping("/stuInfo")
    public AjaxResult stuInfo()
    {
        SrsStuReg srsStuReg = new SrsStuReg();
        srsStuReg.setKSH(getUsername());
        return AjaxResult.success(srsStuRegService.selectSrsStuRegList(srsStuReg));
    }
    //批量导入学生注册信息
    @ApiOperation("学生报道注册信息导入")
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<SrsStuRegImport> util = new ExcelUtil<SrsStuRegImport>(SrsStuRegImport.class);
        List<SrsStuRegImport> srsStuRegImports = util.importExcel(file.getInputStream());
        List<SrsStuReg> srsStuRegs = new ArrayList<>();
        for(SrsStuRegImport x:srsStuRegImports) {
            SrsStuReg srsStuReg = new SrsStuReg();
            srsStuReg.setXSXM(x.getXSXM());
            srsStuReg.setKSH(x.getKSH());
            srsStuReg.setXB(x.getXB());
            srsStuReg.setSFZH(x.getSFZH());
            srsStuReg.setZZMM(x.getZZMM());
            srsStuReg.setMZ(x.getMZ());
            srsStuReg.setLastSchool(x.getLastschool());
            //根据学院获取学院ID
            //Long deptId= sysDeptService.selectDeptIDByNameNew(x.getDeptName());
            //srsStuReg.setDeptId(deptId);
            srsStuReg.setXY(x.getDeptName());
            //根据专业获取专业ID
            //Long majorId= srsMajorsService.getMajorIdByName(x.getZY());
            //srsStuReg.setMajorId(majorId);
            srsStuReg.setZY(x.getZY());
            srsStuReg.setFamNowAddr(x.getFamNowAddr());
            srsStuReg.setSJH(x.getSJH());
            srsStuReg.setRegStatus("0");
            srsStuReg.setCreateTime(DateUtils.getNowDate());
            srsStuReg.setCreateBy(getUsername());
            srsStuReg.setBJ(x.getBJ());
            srsStuReg.setBJDM(x.getBJDM());
            srsStuReg.setDelFlag("0");
            srsStuReg.setSfzAddr(x.getSFZAddr());
            srsStuReg.setIsDirection("0");
            srsStuRegs.add(srsStuReg);
        }
        int count=srsStuRegService.addSrsStuRegs(srsStuRegs);
        if(count>0) {
            System.out.println("导入成功");
        }else{
            System.out.println("导入失败");
        }
        return toAjax(count);

    }
}
