package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.common.GuidGenerator;
import com.lightinit.hsdatashow.common.SysContants;
import com.lightinit.hsdatashow.common.UploadFileUtils;
import com.lightinit.hsdatashow.dictionary.DicEducationType;
import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.model.admin.DicOrganizationType;
import com.lightinit.hsdatashow.model.ms.EducationOrganizationAddModel;
import com.lightinit.hsdatashow.model.ms.EducationOrganizationEditModel;
import com.lightinit.hsdatashow.model.ms.EducationOrganizationResoultModel;
import com.lightinit.hsdatashow.model.ms.EducationOrganizationSearchModel;
import com.lightinit.hsdatashow.service.ms.IMsEducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.List;

import static com.lightinit.hsdatashow.controller.admin.BaseController.buildInvalidResultState;

@Controller
@RequestMapping(value = "/admin/msEducation")
public class MsEducationController {

    @Autowired
    private IMsEducationService msEducationService ;


    @RequestMapping(value = "/index.htm")
    public ModelAndView index(String type){
        ModelAndView modelAndView = new ModelAndView("ms/education/index") ;
        modelAndView.addObject("educationType", DicEducationType.getInstacne().getDicMap()) ;
        modelAndView.addObject("type",type) ;
        modelAndView.addObject("code",msEducationService.getListObj(type)) ;
        return modelAndView ;
    }

    @RequestMapping(value = "/getEducationOrganization.htm")
    public ModelAndView getEducationOrganization(EducationOrganizationSearchModel searchModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("ms/education/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        searchModel.setPage(pager);
        List<EducationOrganizationResoultModel> outputStream = msEducationService.queryList(searchModel);
        pager.setTotal(msEducationService.queryListCount(searchModel));
        modelAndView.addObject("pager",pager);
        modelAndView.addObject("list",outputStream);
        return modelAndView;
    }

    @RequestMapping(value = "/addEducation.htm")
    public ModelAndView addEducation(String orgType){
        ModelAndView modelAndView = new ModelAndView("ms/education/add");
        if(orgType.equals(DicOrganizationType.EDUCATION)){
            modelAndView.addObject("orgTypeList",DicEducationType.getInstacne().getDicMap()) ;
        }
        return modelAndView;
    }

    @RequestMapping(value = "/add.action")
    @ResponseBody
    public ResultState add(EducationOrganizationAddModel educationOrganizationAddModel, MultipartFile fileImg){
        // 保存图片
        String uniqueDir = GuidGenerator.generate() + File.separator;
        String dataFileDir = UploadFileUtils.buildRelativeDir(SysContants.UploadFilePath.EDUCATION_PUBLISH_ROOT_DIR, uniqueDir, SysContants.UploadFilePath.MICROAPP_EDUCATION_ROOT_DIR);
        if (fileImg != null && !fileImg.isEmpty()){
            if (UploadFileUtils.writeUploadFile(fileImg, dataFileDir, SysContants.PropertyKey.UPLOAD_ROOT_DIR)){
                educationOrganizationAddModel.setPhoto(dataFileDir + fileImg.getOriginalFilename());
            }else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        ResultState<Long> resultState = msEducationService.add(educationOrganizationAddModel) ;
        return resultState ;
    }

    @RequestMapping(value = "/editEducation.htm/{id}")
    public ModelAndView editEducation(@PathVariable  long id,String orgType){
        ModelAndView modelAndView = new ModelAndView("ms/education/edit");
        modelAndView.addObject("education",msEducationService.getEducationById(id)) ;
        if(orgType.equals(DicOrganizationType.EDUCATION)){
            modelAndView.addObject("orgTypeList",DicEducationType.getInstacne().getDicMap()) ;
        }
        return modelAndView;
    }

    @RequestMapping(value = "/edit.action")
    @ResponseBody
    public ResultState edit(EducationOrganizationEditModel educationOrganizationEditModel,  MultipartFile fileImg){
        // 保存图片
        String uniqueDir ;
        if (org.apache.commons.lang.StringUtils.isNotEmpty(educationOrganizationEditModel.getPhoto())){
            uniqueDir = UploadFileUtils.getExistingFileRelativeDir(1, educationOrganizationEditModel.getPhoto(), 2);
        } else {
            uniqueDir = GuidGenerator.generate() + File.separator;
        }
        String dataFileDir = UploadFileUtils.buildRelativeDir(SysContants.UploadFilePath.EDUCATION_PUBLISH_ROOT_DIR, uniqueDir, SysContants.UploadFilePath.MICROAPP_EDUCATION_ROOT_DIR);
        if (fileImg != null && !fileImg.isEmpty()){
            if (UploadFileUtils.writeUploadFile(fileImg, dataFileDir, SysContants.PropertyKey.UPLOAD_ROOT_DIR)){
                educationOrganizationEditModel.setPhoto(dataFileDir + fileImg.getOriginalFilename());
            }else {
                return buildInvalidResultState(ResultStateCode.INVALID_DATA, "资源图片保存失败");
            }
        }
        ResultState<Long> resultState = msEducationService.edit(educationOrganizationEditModel) ;
        return resultState ;
    }

    @RequestMapping(value = "/view.htm/{id}")
    public ModelAndView view(@PathVariable  long id){
        ModelAndView modelAndView = new ModelAndView("ms/education/view");
        modelAndView.addObject("education",msEducationService.getEducationById(id)) ;
        return modelAndView;
    }
    @RequestMapping(value = "/delete.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable  long id){
        return msEducationService.delete(id);
    }

}
