package com.yida.data.school.index.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.home.EduHomeFunction;
import com.yida.data.common.core.entity.school.EduSchoolHomeAnnouncement;
import com.yida.data.common.core.entity.school.EduSchoolHomeApp;
import com.yida.data.common.core.entity.school.EduSchoolHomeCover;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicro;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicroIndex;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicroPic;
import com.yida.data.common.core.entity.school.EduSchoolHomeNews;
import com.yida.data.common.core.entity.school.EduSchoolManuscript;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.index.service.EduDeptHomeAppService;
import com.yida.data.school.index.service.EduHomeFunctionService;
import com.yida.data.school.index.service.EduSchoolHomeAnnouncementService;
import com.yida.data.school.index.service.EduSchoolHomeAppService;
import com.yida.data.school.index.service.EduSchoolHomeCoverService;
import com.yida.data.school.index.service.EduSchoolHomeMicroIndexService;
import com.yida.data.school.index.service.EduSchoolHomeMicroPicService;
import com.yida.data.school.index.service.EduSchoolHomeMicroService;
import com.yida.data.school.index.service.EduSchoolHomeNewsService;
import com.yida.data.school.news.service.EduSchoolManuscriptService;
import com.yida.data.school.vo.index.HomeInfoVO;
import com.yida.data.school.vo.index.SchoolIndexVO;

import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Controller
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Api(tags = "校园主页")
@Slf4j
@RestController
@RequestMapping("/index/home")
@RequiredArgsConstructor
public class EduSchoolHomeController {

    private final EduHomeFunctionService eduHomeFunctionService;
    private final EduSchoolHomeCoverService eduSchoolHomeCoverService;
    private final EduSchoolHomeAppService eduSchoolHomeAppService;
    private final EduSchoolHomeNewsService eduSchoolHomeNewsService;
    private final EduSchoolHomeAnnouncementService eduSchoolHomeAnnouncementService;
    private final EduSchoolHomeMicroService eduSchoolHomeMicroService;
    private final EduSchoolHomeMicroIndexService eduSchoolHomeMicroIndexService;
    private final EduSchoolHomeMicroPicService eduSchoolHomeMicroPicService;
    private final EduSchoolManuscriptService eduSchoolManuscriptService;
    private final EduDeptHomeAppService eduDeptHomeAppService;

    @ApiOperation("主页信息查询")
    @GetMapping("/getHomeInfo")
    public ResultBean<HomeInfoVO> getHomeInfo(@ApiParam("学校id") Long schoolId, @RequestParam(required = false) Integer userType) {
        if (schoolId == null) {
            schoolId = FebsUtil.getTopDeptId();
        }
        return ResultBean.buildSuccess(eduSchoolHomeCoverService.getHomeInfo(schoolId, userType));
    }

    @ApiOperation("更新轮播图(新增、编辑、删除)")
    @PostMapping("/updateCover")
    public ResultBean updateCover(@RequestBody List<EduSchoolHomeCover> coverList) {
        eduSchoolHomeCoverService.updateCover(coverList, FebsUtil.getTopDeptId());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("应用新增编辑")
    @PostMapping("/saveApp")
    public ResultBean saveApp(@RequestBody List<EduSchoolHomeApp> app) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            for (EduSchoolHomeApp eduSchoolHomeApp : app) {
                eduSchoolHomeApp.setSchoolId(currentUser.getDeptId());
            }
            eduSchoolHomeAppService.saveApp(app);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询应用")
    @GetMapping("/listApp")
    public ResultBean<List<EduSchoolHomeApp>> listApp(@ApiParam("应用类型，0-家长，1-教师") Integer type,
        @ApiParam("学校id") Long schoolId,
        @ApiParam("家长或教职工id") @RequestParam(required = false) Long identityId) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            schoolId = currentUser.getDeptId();
        }
        return ResultBean.buildSuccess(eduSchoolHomeAppService.listApp(schoolId, type, identityId));
    }

    @ApiOperation("删除应用")
    @PostMapping("/delAppBatch")
    public ResultBean delAppBatch(@RequestBody List<Long> ids) {
        eduSchoolHomeAppService.delAppBatch(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("公告区保存、修改")
    @PostMapping("/saveAnnouncement")
    public ResultBean updateAnnouncement(@RequestBody EduSchoolHomeAnnouncement announcement) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            announcement.setSchoolId(currentUser.getDeptId());
            eduSchoolHomeAnnouncementService.saveAnnouncement(announcement);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("微官网-修改")
    @PostMapping("/saveMicro")
    public ResultBean saveMicro(@RequestBody EduSchoolHomeMicro micro) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            micro.setSchoolId(currentUser.getDeptId());
            eduSchoolHomeMicroService.saveMicro(micro);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("微官网-学校概况")
    @PostMapping("/saveMicroSchoolIndex")
    public ResultBean saveMicroSchoolIndex(@RequestBody EduSchoolHomeMicroIndex index) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            index.setSchoolId(currentUser.getDeptId());
            eduSchoolHomeMicroIndexService.saveIndex(index);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("微官网-栏目内容修改")
    @PostMapping("/saveMicroColPic")
    public ResultBean saveMicroColPic(@RequestBody List<EduSchoolHomeMicroPic> picList) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            eduSchoolHomeMicroPicService.savePic(picList);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询校园概况")
    @GetMapping("/getMicroSchoolIndex")
    public ResultBean<SchoolIndexVO> getMicroSchoolIndex(@ApiParam("栏目id") @RequestParam(required = false) Long colId) {
        if (colId != null) {
            return ResultBean.buildSuccess(eduSchoolHomeMicroIndexService.getInfo(colId));
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据栏目id查询图片")
    @GetMapping("/listPicByCol")
    public ResultBean<List<EduSchoolHomeMicroPic>> listPicByCol(@ApiParam("栏目id") Long colId) {
        return ResultBean.buildSuccess(eduSchoolHomeMicroPicService.list(Wrappers.lambdaQuery(new EduSchoolHomeMicroPic())
            .eq(EduSchoolHomeMicroPic::getColId, colId)
            .orderByAsc(EduSchoolHomeMicroPic::getSort)));
    }

    @ApiOperation("新闻区修改")
    @PostMapping("/saveNews")
    public ResultBean saveNews(@RequestBody EduSchoolHomeNews news) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            news.setSchoolId(currentUser.getDeptId());
            eduSchoolHomeNewsService.saveNews(news);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取置顶公告")
    @GetMapping("/getTopAnnouncement")
    public ResultBean getTopAnnouncement(Long schoolId) {
        EduSchoolHomeAnnouncement announcement = eduSchoolHomeAnnouncementService
            .getOne(Wrappers.lambdaQuery(new EduSchoolHomeAnnouncement()).eq(EduSchoolHomeAnnouncement::getSchoolId, schoolId));
        if (announcement != null && announcement.getTopAnnouncementId() != null) {
            EduSchoolManuscript top = eduSchoolManuscriptService.getById(announcement.getTopAnnouncementId());
            return ResultBean.buildSuccess(top);
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("修改功能区")
    @PostMapping("/updateFunction")
    public ResultBean updateFunction(@RequestBody EduHomeFunction functionData) {
        functionData.setDeptId(FebsUtil.getTopDeptId());
        eduHomeFunctionService.updateFunction(functionData);
        return ResultBean.buildSuccess();
    }


    @GetMapping("/getWxPublicURLLink")
    @ApiOperation("获取微信小程序短链接")
    ResultBean<String> getWxPublicURLLink(
        @RequestParam @ApiParam(value = "学校ID", required = true) String schoolId,
        @RequestParam @ApiParam(value = "用户ID", required = true) Long userId,
        @RequestParam @ApiParam(value = "用户类型", required = true) Integer userType,
        @RequestParam @ApiParam(value = "系统内部小程序ID", required = true) Long localAppId) {
        return ResultBean.buildSuccess(eduDeptHomeAppService.getWxPublicURLLink(schoolId, userId, userType, localAppId));
    }
}
