package com.ruoyi.web.controller.school;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbSchool;
import com.ruoyi.system.service.ITbSchoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 学校信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@Controller
@RequestMapping("/system/school")
public class TbSchoolController extends BaseController
{
    private String prefix = "system/school";

    @Autowired
    private ITbSchoolService tbSchoolService;

    @RequiresPermissions("system:school:view")
    @GetMapping()
    public String school()
    {
        return prefix + "/school";
    }

    /**
     * 查询学校信息列表
     */
    @RequiresPermissions("system:school:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbSchool tbSchool)
    {
        startPage();
        List<TbSchool> list = tbSchoolService.selectTbSchoolList(tbSchool);
        return getDataTable(list);
    }

    /**
     * @Author : fanxiaolong
     * @Description :  分页查询所有未通过审核或待审核的学校信息
     * @DateTime : 2022/9/2 17:09
     * @param : tbSchool
     * @return : com.ruoyi.common.core.page.TableDataInfo
     */
    @RequiresPermissions("system:school:list")
    @PostMapping("/notPassList")
    @ResponseBody
    public TableDataInfo selectTbSchoolNotPassList(TbSchool tbSchool){

        startPage();
        List<TbSchool> list = tbSchoolService.selectTbSchoolNotPassList(tbSchool);
        return getDataTable(list);
    }

    /**
     * 查询学校信息列表
     */
    @RequiresPermissions("system:school:list")
    @GetMapping("/getList")
    @ResponseBody
    public List<TbSchool> getList()
    {
        List<TbSchool> list = tbSchoolService.selectAllTbSchoolList();
        return list;
    }

    /**
     * 导出学校信息列表
     */
    @RequiresPermissions("system:school:export")
    @Log(title = "学校信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbSchool tbSchool)
    {
        List<TbSchool> list = tbSchoolService.selectTbSchoolList(tbSchool);
        ExcelUtil<TbSchool> util = new ExcelUtil<TbSchool>(TbSchool.class);
        return util.exportExcel(list, "学校信息数据");
    }

    /**
     * 新增学校信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        TbSchool tbSchool=new TbSchool();
        mmap.put("school",tbSchool);
        return prefix + "/add";
//        return prefix + "/test1";
    }

    /**
     * 新增保存学校信息
     */
//    @RequiresPermissions("system:school:add")
//    @Log(title = "学校信息", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(@RequestParam(value="schoolLogo",required=false) MultipartFile file,@RequestParam(value="backgroundImage",required=false) MultipartFile file1)
//    {
//        TbSchool tbSchool =new TbSchool();
//        try {
//            if (file != null && file1 != null) {
//                // 上传文件路径
//                String filePath = RuoYiConfig.getUploadPath();
//                // 上传并返回新文件名称
//                String fileName = null;
//                fileName = FileUploadUtils.upload(filePath, file);
//                tbSchool.setSchoolLogo(fileName);
//                fileName = FileUploadUtils.upload(filePath, file1);
//                tbSchool.setBackgroundImage(fileName);
//                tbSchool.setCreator(getUserId());
//                tbSchool.setCheckStatus(1);
//                setParameters(tbSchool,getRequest());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return toAjax(tbSchoolService.insertTbSchool(tbSchool));
//    }

    @RequiresPermissions("system:school:add")
    @Log(title = "学校信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbSchool tbSchool)
    {
        tbSchool.setCreator(getUserId());
        return toAjax(tbSchoolService.insertTbSchool(tbSchool));
    }

    /**
     * 修改学校信息
     */
    @RequiresPermissions("system:school:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbSchool tbSchool = tbSchoolService.selectTbSchoolById(id);
        mmap.put("tbSchool", tbSchool);
        return prefix + "/edit";
    }

    /**
     * 修改保存学校信息
     */
    @RequiresPermissions("system:school:edit")
    @Log(title = "学校信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbSchool tbSchool)
    {
        tbSchool.setModifiedBy(getUserId());
        return toAjax(tbSchoolService.updateTbSchool(tbSchool));
    }
//    public AjaxResult editSave(@RequestParam(value="schoolLogo",required=false) MultipartFile file,
//                               @RequestParam(value="backgroundImage",required=false) MultipartFile file1)
//    {
//        TbSchool tbSchool =new TbSchool();
//        try {
//            tbSchool.setId(Long.parseLong(getRequest().getParameter("id")));
//            setParameters(tbSchool,getRequest());
//            tbSchool.setModifiedBy(getUserId());
//            if (file != null) {
//                // 上传文件路径
//                String filePath = RuoYiConfig.getUploadPath();
//                // 上传并返回新文件名称
//                String fileName = null;
//                fileName = FileUploadUtils.upload(filePath, file);
//                tbSchool.setSchoolLogo(fileName);
//            }
//            if (file1 != null) {
//                // 上传文件路径
//                String filePath = RuoYiConfig.getUploadPath();
//                // 上传并返回新文件名称
//                String fileName = null;
//                fileName = FileUploadUtils.upload(filePath, file1);
//                tbSchool.setBackgroundImage(fileName);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return toAjax(tbSchoolService.updateTbSchool(tbSchool));
//    }

    public void setParameters(TbSchool tbSchool, HttpServletRequest request){
        tbSchool.setSchoolCnName(request.getParameter("schoolCnName"));
        tbSchool.setSchoolEnName(request.getParameter("schoolEnName"));
        tbSchool.setSchoolCnCountry(request.getParameter("schoolCnCountry"));
        tbSchool.setSchoolCnCity(request.getParameter("schoolCnCity"));
//        tbSchool.setSchoolEnCountry(request.getParameter("schoolEnCountry"));
//        tbSchool.setSchoolEnCity(request.getParameter("schoolEnCity"));
        tbSchool.setSchoolType(Integer.parseInt(request.getParameter("schoolType")));
        tbSchool.setSchoolCampus(request.getParameter("schoolCampus"));
        tbSchool.setQsWorld(request.getParameter("qsWorld"));
        tbSchool.setQsArea(request.getParameter("qsArea"));
        tbSchool.setSchoolIntroduction(request.getParameter("schoolIntroduction"));
        tbSchool.setSchoolArea(request.getParameter("schoolArea"));
        tbSchool.setLiveType(request.getParameter("liveType"));
        tbSchool.setAirportPickup(request.getParameter("airportPickup"));
        tbSchool.setQsEmployability(request.getParameter("qsEmployability"));
        tbSchool.setEmploymentRate(getRequest().getParameter("employmentRate"));
        tbSchool.setTeachingLanguage(getRequest().getParameter("teachingLanguage"));
        tbSchool.setInternship(getRequest().getParameter("internship"));
        tbSchool.setGovAuth(getRequest().getParameter("govAuth"));
    }


    /**
     * 删除学校信息
     */
    @RequiresPermissions("system:school:remove")
    @Log(title = "学校信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbSchoolService.deleteTbSchoolByIds(ids));
    }

    /**
     * 新增学校信息
     */
//    @GetMapping("/uploadPhoto")
//    public String uploadPhoto()
//    {
//        return prefix + "/test";
//    }

    @GetMapping("/uploadImages")
    public String uploadImages(ModelMap mmap)
    {
        TbSchool tbSchool=new TbSchool();
        mmap.put("school",tbSchool);
        return prefix + "/uploadSchoolCover";
    }

    @Log(title = "学校信息", businessType = BusinessType.INSERT)
    @PostMapping("/uploadImage")
    @ResponseBody
    public AjaxResult updateImage(@RequestParam("imageFile") MultipartFile file)
    {
        try
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/uploadFileName")
    public AjaxResult uploadFileName(@RequestParam("schoolCover") String schoolCover){
        AjaxResult ajax = AjaxResult.success();
        System.out.println(">>>>>>>>>>>>>>>>>>>"+schoolCover);
        TbSchool school=new TbSchool();
        school.setSchoolCover(schoolCover);
        ajax.put("school", school);
        return ajax;
    }
}
