package cn.chnmuseum.party.web.controller;

import cn.chnmuseum.party.auth.SHA256PasswordEncryptionService;
import cn.chnmuseum.party.auth.util.AESUtils;
import cn.chnmuseum.party.auth.util.JwtTokenUtil;
import cn.chnmuseum.party.common.enums.AuditOperationEnum;
import cn.chnmuseum.party.common.enums.FileCatEnum;
import cn.chnmuseum.party.common.enums.FileTypeEnum;
import cn.chnmuseum.party.common.enums.LanguageEnum;
import cn.chnmuseum.party.common.util.TimeUtils;
import cn.chnmuseum.party.common.validator.groups.Add;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.*;
import cn.chnmuseum.party.service.*;
import cn.chnmuseum.party.service.impl.*;
import cn.chnmuseum.party.web.controller.base.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@RestController()
@RequestMapping("/cmRestApi")
@Api(tags = {"中国移动API"})
public class ChinaMobileRestApiController extends BaseController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private TUserServiceImpl userService;

    @Resource
    private TOrganServiceImpl organService;

    @Resource
    private RunLogServiceImpl runLogService;

    @Resource
    private AssetServiceImpl assetService;

    @Resource
    private VideoContentService videoContentService;

    @Resource
    private TBoxOperationServiceImpl boxOperationService;

    @Resource
    private ExhibitionBoardService exhibitionBoardService;

    @Resource
    private TBoardStatisticService tBoardStatisticService;

    @Resource
    private LearningProjectService learningProjectService;

    @Resource
    private LearningContentService learningContentService;

    @Resource
    private ExhibitionBoardCatService exhibitionBoardCatService;

    @Resource
    private CopyrightOwnerService copyrightOwnerService;

    @Resource
    private LearningContentBoardService learningContentBoardService;

    @Resource
    private TAppVersionService appVersionService;

    @Resource
    private TAppDirPicService appDirPicService;

    @Resource
    private TAppRunPicService appRunPicService;

    @Resource
    private LearningContentAssetService learningContentAssetService;

    private static final String SHIRO_JWT_TOKEN = "shiro:jwt:token:";
    //用户登录次数计数  redisKey 前缀
    private static final String SHIRO_LOGIN_COUNT = "shiro_login_count_";
    //用户登录是否被锁定    一小时 redisKey 前缀
    private static final String SHIRO_IS_LOCK = "shiro_is_lock_";

    @ApiOperation(value = "获取机顶盒密钥", notes = "获取机顶盒密钥")
    @GetMapping("/equitment/key")
    @RequiresAuthentication  //@RequiresPermissions("equitment:key")
    public Map<String, Object> getBoxPrivateKey(@RequestParam(value = "mac") String mac) {
        try {
            String macAddress = mac.trim().toUpperCase().replaceAll("-|_", ":");
            final LambdaQueryWrapper<TBoxOperation> queryWrapper = Wrappers.<TBoxOperation>lambdaQuery().eq(TBoxOperation::getMac, macAddress);
            final TBoxOperation tBoxOperation = this.boxOperationService.getOne(queryWrapper);
            if (tBoxOperation != null) {
                final String organId = tBoxOperation.getOrganId();
                final TUser tUser = getcurUser();
                if (tUser != null && organId.equals(tUser.getOrgId())) {
//                    final String countStr = this.stringRedisTemplate.opsForValue().get(macAddress + "_count");
//                    if (StringUtils.isNotBlank(countStr)) {
//                        final long count = Long.parseLong(countStr);
//                        if (count > 1) {
//                            JSONObject jsonObject = new JSONObject();
//                            jsonObject.put("key", "");
//                            return getFailResult("400", "已经获取过一次,无法再次获取", jsonObject);
//                        }
//                    } else if (tBoxOperation.getRequestCount() > 1) {
//                        JSONObject jsonObject = new JSONObject();
//                        jsonObject.put("key", "");
//                        return getFailResult("400", "已经获取过一次,无法再次获取", jsonObject);
//                    }
//                    this.stringRedisTemplate.opsForValue().increment(macAddress + "_count");
//                    tBoxOperation.setRequestCount(tBoxOperation.getRequestCount() + 1);
//                    this.boxOperationService.updateById(tBoxOperation);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("key", tBoxOperation.getPrivateKey());
                    return getResult(jsonObject);
                } else {
                    return getFailResult("1109", "账号认证失败");
                }
            }
            return getFailResult("400", "系统未查询到此机顶盒相关信息");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
    }

    @ApiOperation(value = "机顶盒激活状态查询", notes = "机顶盒激活状态查询")
    @GetMapping("/equitment/activity")
    //@RequiresAuthentication  //@RequiresPermissions("/equitment/activity/")
    public Map<String, Object> getActivity(@RequestParam(required = true) String mac) {
        try {
            UpdateWrapper<TBoxOperation> wrapper = new UpdateWrapper<>();
            wrapper.eq("mac", mac.toUpperCase());
            TBoxOperation one = boxOperationService.getOne(wrapper);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("activityStatus", false);
            if (one != null) {
                map.put("activityStatus", true);
            }
            return getResult(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
    }

    @RequestMapping(value = "/user/webLogin", method = RequestMethod.POST)
    public Map<String, Object> login(@RequestParam(value = "username") String username,
                                     @RequestParam(value = "password") String password,
                                     @RequestParam(value = "mac") String mac) {
        JSONObject resultMap = new JSONObject(true);
        TUser user;
        if (StringUtils.isNoneBlank(username)) {
            try {
                //访问一次,计数一次
//                ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
//                if ("LOCK".equals(opsForValue.get(SHIRO_IS_LOCK + username))) {
//                    resultMap.put("resultCode", "400");
//                    resultMap.put("message", "由于密码输入错误次数大于5次,12小时内帐号已禁止登录!请您联系相关管理人员,联系电话:13924551212,邮箱:325346534@zh.com。");
//                    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
//                }
                user = userService.selectByUsername(username);
                if (user == null) {
                    resultMap.put("resultCode", "400");
                    resultMap.put("message", "用户名或密码不正确!");
                    return resultMap;
                }
                TBoxOperation operation = boxOperationService.getOne(new UpdateWrapper<TBoxOperation>().eq("organ_id", user.getOrgId()));
                if (operation == null || "".equals(operation.getMac())) {
                    resultMap.put("resultCode", "400");
                    resultMap.put("message", "用户未激活!");
                    return resultMap;
                }
                //解密
                mac = AESUtils.aesDecrypt(mac);
                password = AESUtils.aesDecrypt(password);

                if (!mac.toUpperCase().equals(operation.getMac().toUpperCase())) {
                    resultMap.put("resultCode", "400");
                    resultMap.put("message", "mac地址不正确!");
                    return resultMap;
                }
                if (AuditOperationEnum.DISABLE.name().equals(user.getStatus())) {
                    resultMap.put("resultCode", "400");
                    resultMap.put("message", "此帐号已禁用,请联系管理员!");
                    return resultMap;
                }

                if (user.getPermanent() != null && !user.getPermanent()) {
                    if (user.getEffectiveDate().isAfter(LocalDate.now()) || user.getExiredDate().isBefore(LocalDate.now())) {
                        resultMap.put("resultCode", "400");
                        resultMap.put("message", "此帐号已失效,请联系管理员!");
                        return resultMap;
                    }
                }

                byte[] salt = user.getPasswordSalt();
                if (!new String(SHA256PasswordEncryptionService.createPasswordHash(password, salt)).equals(new String(user.getPasswordHash()))) {
//                    opsForValue.increment(SHIRO_LOGIN_COUNT + username, 1);
//                    //计数大于5时,设置用户被锁定一小时
//                    String s = opsForValue.get(SHIRO_LOGIN_COUNT + username);
//                    if (StringUtils.isNotBlank(s)) {
//                        if (Integer.parseInt(s) >= 5) {
//                            opsForValue.set(SHIRO_IS_LOCK + username, "LOCK");
//                            stringRedisTemplate.expire(SHIRO_IS_LOCK + username, 12, TimeUnit.HOURS);
//                        }
//                    }
                    resultMap.put("resultCode", "400");
                    resultMap.put("message", "用户名或密码不正确!");
                    return resultMap;
                }
                String token = JwtTokenUtil.sign(username, user.getId());
                // 将token信息存入Redis
                stringRedisTemplate.opsForValue().set(SHIRO_JWT_TOKEN + token, user.getId(), 240, TimeUnit.MINUTES);

                JSONObject jsonObject = new JSONObject(true);
                jsonObject.put("token", token);
                jsonObject.put("userId", user.getId());
                jsonObject.put("userName", user.getUserName());
                jsonObject.put("expire", TimeUtils.format(LocalDateTime.now().plusMinutes(240), TimeUtils.FORMAT_ONE));
                jsonObject.put("expireDate", user.getExiredDate());
                jsonObject.put("orgId", user.getOrgId());
                jsonObject.put("orgCode", user.getOrgCode());
                jsonObject.put("orgName", user.getOrgName());

                resultMap.put("resultCode", "200");
                resultMap.put("message", "登录成功");
                resultMap.put("data", jsonObject);
                return resultMap;
            } catch (Exception e) {
                resultMap.put("resultCode", "500");
                resultMap.put("message", e.getMessage());
            }
        }
        return getFailResult(resultMap.get("message"));
    }

    @ApiOperation(value = "获取单个成员信息")
    @GetMapping(value = "/user/getUserInfo")
    @RequiresAuthentication
    public Map<String, Object> getById(String userId) {
        JSONObject resultMap = new JSONObject(true);
        try {
            TUser user = userService.getById(userId);
            TOrgan organ = organService.getById(user.getOrgId());

            if (null == user) {
                resultMap.put("resultCode", "400");
                resultMap.put("message", "用户不存在");
                resultMap.put("data", "");
                return resultMap;
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("userId", user.getId());
            map.put("userName", user.getUserName());
//            long expire = stringRedisTemplate.getExpire(SHIRO_JWT_TOKEN + token) == null ? 0L : stringRedisTemplate.getExpire(SHIRO_JWT_TOKEN + token);
            //jsonObject.put("expire", TimeUtils.format(LocalDateTime.now().plusMinutes(expire), TimeUtils.FORMAT_ONE));
            map.put("effectiveDate", user.getEffectiveDate());
            map.put("expireDate", user.getExiredDate());
            map.put("orgCode", user.getOrgId());
            map.put("orgName", organ.getName());

            resultMap.put("resultCode", "200");
            resultMap.put("message", "成功");
            resultMap.put("data", map);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("resultCode", "500");
            resultMap.put("message", "获取单个成员信息失败!");
        }
        return getFailResult();
    }

    @RequestMapping(value = "/user/logout", method = RequestMethod.GET)
    @RequiresAuthentication
    public Map<String, Object> logout() {
        String token = request.getHeader("Authorization");
        JSONObject resultMap = new JSONObject(true);
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        try {
            if (StringUtils.isNotBlank(token)) {
//                SecurityUtils.getSubject().logout();

                this.stringRedisTemplate.delete(SHIRO_JWT_TOKEN + token);
            }
            resultMap.put("resultCode", "200");
            resultMap.put("message", "成功");
            resultMap.put("data", "");
            return resultMap;
        } catch (Exception e) {
            resultMap.put("resultCode", "500");
            resultMap.put("message", "注销错误!");
        }
        return getFailResult("注销错误!");
    }

    /**
     * 插入机顶盒日志表
     */
    @ApiOperation(value = "插入机顶盒日志表", notes = "插入机顶盒日志表")
    @PostMapping(value = "/equitment/runLog")
    @RequiresAuthentication
    public Map<String, Object> insertRunLog(RunLog runLog) {
        JSONObject resultMap = new JSONObject();

        try {
            boolean b = runLogService.insertRunLog(runLog);
            resultMap.put("resultCode", "200");
            resultMap.put("message", "成功");
            resultMap.put("data", "");
            return resultMap;
        } catch (Exception e) {
            resultMap.put("resultCode", "500");
            resultMap.put("message", "失败");
            resultMap.put("data", "");
        }
        return getFailResult();
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "boardId", value = "展板内容ID", paramType = "query", dataType = "String", required = true)
    })
    @ApiOperation(value = "查询展板视频/语音导览语言列表", notes = "查询展板视频/语音导览语言列表")
    @GetMapping(value = "/exhibitionBoard/language")
    @RequiresAuthentication
    public Map<String, Object> getExhibitionBoardVideoLanguageList(@RequestParam(value = "boardId") String boardId) {
        JSONObject resultMap = new JSONObject();
        Map<String, List<Map<String, String>>> langMap = new LinkedHashMap<>(2);
        try {
            List<Map<String, String>> videoList = new ArrayList<>();
            final ExhibitionBoard exhibitionBoard = this.exhibitionBoardService.getById(boardId);
            final String videoContentId = exhibitionBoard.getVideoContentId();
            LambdaQueryWrapper<Asset> assetLambdaQueryWrapper;
            List<Asset> assetList = new ArrayList<>();
            List<String> languageList = new ArrayList<>();
            if (StringUtils.isNotEmpty(videoContentId)) {
                assetLambdaQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, videoContentId).eq(Asset::getPublished, true);
                assetList = this.assetService.list(assetLambdaQueryWrapper);
                languageList = assetList.stream().map(Asset::getLanguage).collect(Collectors.toList());
                for (LanguageEnum languageEnum : LanguageEnum.values()) {
                    if (languageList.contains(languageEnum.name())) {
                        Map<String, String> map = new LinkedHashMap<>(2);
                        map.put("code", languageEnum.name());
                        map.put("name", languageEnum.getName());
                        videoList.add(map);
                    }
                }
                assetLambdaQueryWrapper.clear();
            }
            langMap.put("video", videoList);


            List<Map<String, String>> audioList = new ArrayList<>();
            assetLambdaQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, boardId);
            assetLambdaQueryWrapper.eq(Asset::getPublished, true);
            assetLambdaQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_AUDIO);
            assetList = this.assetService.list(assetLambdaQueryWrapper);
            languageList = assetList.stream().map(Asset::getLanguage).collect(Collectors.toList());
            for (LanguageEnum languageEnum : LanguageEnum.values()) {
                if (languageList.contains(languageEnum.name())) {
                    Map<String, String> map = new LinkedHashMap<>(2);
                    map.put("code", languageEnum.name());
                    map.put("name", languageEnum.getName());
                    audioList.add(map);
                }
            }
            langMap.put("audio", audioList);

            resultMap.put("resultCode", "200");
            resultMap.put("message", "成功");
            resultMap.put("data", langMap);
            return resultMap;
        } catch (Exception e) {
            resultMap.put("resultCode", "400");
            resultMap.put("message", "该展板已下架");
            resultMap.put("data", "");
        }
        return getFailResult("400", "该展板已下架");
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "learningProjectId", value = "学习项目ID", paramType = "query", dataType = "String")
    })
    @PostMapping("/exhibitionBoard/getPage")
    @RequiresAuthentication  //@RequiresPermissions("exhibition:board:page")
    @ApiOperation(value = "总展板列表查询", notes = "总展板列表查询")
    public Map<String, Object> getExhibitionBoardPageList(@RequestParam(value = "learningProjectId", required = false) String learningProjectId) {
        // 要求如果展板被删除,返回最新创建的展板
        LearningProject project = this.learningProjectService.getById(learningProjectId);
        if (project == null) {
            LambdaQueryWrapper<LearningProject> last = Wrappers.<LearningProject>lambdaQuery().orderByDesc(LearningProject::getCreateTime).last("LIMIT 1");
            LearningProject one = this.learningProjectService.getOne(last);
            learningProjectId = one.getId();
        }
        //中移要求,如果翻页参数超出数据总数,返回第一页
        Page<ExhibitionBoard> page1 = getPage();
        long l = page1.getCurrent() * page1.getSize() - 10;
        int size = exhibitionBoardService.getList(learningProjectId, true).size();
        if (l > size) {
            page1 = new Page<>(1, 10);
        }

        final IPage<ExhibitionBoard> page = this.learningContentBoardService.getBoardPageByLearningProjectId(page1, learningProjectId, null);
        for (ExhibitionBoard exhibitionBoard : page.getRecords()) {
            exhibitionBoard.setLearningProjectId(learningProjectId);
            // 展板的视频内容版权方
            if (StringUtils.isNotEmpty(exhibitionBoard.getVideoContentCopyrightOwnerId())) {
                String name = this.copyrightOwnerService.getById(exhibitionBoard.getVideoContentCopyrightOwnerId()).getName();
                exhibitionBoard.setBoardCopyrightOwnerName(name);
            }
            if (exhibitionBoard.getExhibitionBoardCatId() != null) {
                String name = this.exhibitionBoardCatService.getById(exhibitionBoard.getExhibitionBoardCatId()).getName();
                exhibitionBoard.setExhibitionBoardCatName(name);
            }
            LambdaQueryWrapper<Asset> assetQueryWrapper;
            if (StringUtils.isNotEmpty(exhibitionBoard.getVideoContentId())) {
                final VideoContent videoContent = this.videoContentService.getById(exhibitionBoard.getVideoContentId());
                if (videoContent != null) {
                    exhibitionBoard.setBoardVideoContentThumbnail(videoContent.getThumbnail());
                }

                assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoard.getVideoContentId());
                assetQueryWrapper.eq(Asset::getPublished, true);
                assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.VIDEO_CONTENT.name());
                List<Asset> videoList = this.assetService.list(assetQueryWrapper);
                for (Asset asset : videoList) {
                    asset.setExhibitionBoardName(exhibitionBoard.getName());
                    asset.setExhibitionBoardId(exhibitionBoard.getId());
                }
                exhibitionBoard.setVideoList(videoList);
                assetQueryWrapper.clear();
            }

            assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoard.getId());
            assetQueryWrapper.eq(Asset::getPublished, true);
            assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_DATUM.name());
            List<Asset> datumList = this.assetService.list(assetQueryWrapper);
            for (Asset asset : datumList) {
                asset.setExhibitionBoardName(exhibitionBoard.getName());
                asset.setExhibitionBoardId(exhibitionBoard.getId());
            }
            exhibitionBoard.setDatumList(datumList);

//            videoList.addAll(datumList.stream().filter(x -> FileTypeEnum.VIDEO.name().equalsIgnoreCase(x.getFileType())).collect(Collectors.toList()));
            // 填充编号
            setBoardSerial(exhibitionBoard);

        }
        return getResult(page);
    }


    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "learningContentId", value = "学习内容ID", paramType = "query", dataType = "String")
    })
    @PostMapping("/exhibitionBoard/getByContentPage")
    @RequiresAuthentication  //@RequiresPermissions("exhibition:board:page")
    @ApiOperation(value = "根据学习内容查询展板列表", notes = "根据学习内容查询展板列表")
    public Map<String, Object> getByContentExhibitionBoardPageList(@RequestParam(value = "learningContentId", required = false) String learningContentId) {
        //中移要求,如果翻页参数超出数据总数,返回第一页
        Page<ExhibitionBoard> page1 = getPage();
        long l = page1.getCurrent() * page1.getSize() - 10;
        int size = exhibitionBoardService.getByContentList(learningContentId, true).size();
        if (l > size) {
            page1 = new Page<>(1, 10);
        }
        final IPage<ExhibitionBoard> page = this.learningContentBoardService.getBoardPageByLearningContentId(page1, learningContentId, null);
        for (ExhibitionBoard exhibitionBoard : page.getRecords()) {
            exhibitionBoard.setLearningContentId(learningContentId);
            if (exhibitionBoard.getExhibitionBoardCatId() != null) {
                String name = this.exhibitionBoardCatService.getById(exhibitionBoard.getExhibitionBoardCatId()).getName();
                exhibitionBoard.setExhibitionBoardCatName(name);
            }
            if (StringUtils.isNotEmpty(exhibitionBoard.getVideoContentCopyrightOwnerId())) {
                String name = this.copyrightOwnerService.getById(exhibitionBoard.getVideoContentCopyrightOwnerId()).getName();
                exhibitionBoard.setBoardCopyrightOwnerName(name);
            }
            if (StringUtils.isEmpty(exhibitionBoard.getCover())) {
                exhibitionBoard.setCover("");
            }
            LambdaQueryWrapper<Asset> assetQueryWrapper;
            if (StringUtils.isNotEmpty(exhibitionBoard.getVideoContentId())) {
                final VideoContent videoContent = this.videoContentService.getById(exhibitionBoard.getVideoContentId());
                if (videoContent != null) {
                    exhibitionBoard.setBoardVideoContentThumbnail(videoContent.getThumbnail());
                }

                assetQueryWrapper = Wrappers.<Asset>lambdaQuery();
                assetQueryWrapper.eq(Asset::getRefItemId, exhibitionBoard.getVideoContentId());
                assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.VIDEO_CONTENT.name());
                assetQueryWrapper.eq(Asset::getPublished, true);
                List<Asset> videoList = this.assetService.list(assetQueryWrapper);
                videoList.forEach(asset ->{
                    asset.setExhibitionBoardName(exhibitionBoard.getName());
                    asset.setExhibitionBoardId(exhibitionBoard.getId());
                });

                exhibitionBoard.setVideoList(videoList);
                assetQueryWrapper.clear();
            }

            // 该学习内容下的展板参考资料哪些显示
            LambdaQueryWrapper<LearningContentAsset> wrapper = Wrappers.<LearningContentAsset>lambdaQuery().eq(LearningContentAsset::getLearningContentId, learningContentId);
            wrapper.eq(LearningContentAsset::getBoardId, exhibitionBoard.getId());
            wrapper.select(LearningContentAsset::getAssetId);
            List<String> list = this.learningContentAssetService.listObjs(wrapper,Object::toString);

            if (!CollectionUtils.isEmpty(list)) {
                assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoard.getId());
                assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_DATUM.name());
                assetQueryWrapper.eq(Asset::getPublished, true);
                assetQueryWrapper.in(Asset::getId, list);
                List<Asset> datumList = this.assetService.list(assetQueryWrapper);
                for (Asset asset : datumList) {
                    asset.setExhibitionBoardId(exhibitionBoard.getId());
                    asset.setExhibitionBoardName(exhibitionBoard.getName());
                }
                exhibitionBoard.setDatumList(datumList);
            }

            //videoList.addAll(datumList.stream().filter(x -> FileTypeEnum.VIDEO.name().equalsIgnoreCase(x.getFileType())).collect(Collectors.toList()));

            // 填充编号
            this.setBoardSerial(exhibitionBoard);
        }
        return getResult(page);
    }

    @ApiOperation(value = "展板详情查询", notes = "展板详情查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boardId", value = "展板ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "videoLanguage", value = "语言", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "audioLanguage", value = "语言", dataType = "String", paramType = "query")
    })
    @GetMapping("/exhibitionBoard/getBoardInfo")
    @RequiresAuthentication
    public Map<String, Object> getById(@RequestParam(value = "boardId") String id,
                                       @RequestParam(value = "videoLanguage", required = false) LanguageEnum videoLanguage,
                                       @RequestParam(value = "audioLanguage", required = false) LanguageEnum audioLanguage) {
        ExhibitionBoard exhibitionBoard = exhibitionBoardService.getById(id);
        String boardCopyrightOwnerId = exhibitionBoard.getBoardCopyrightOwnerId();
        if (boardCopyrightOwnerId != null) {
            exhibitionBoard.setBoardCopyrightOwnerName(this.copyrightOwnerService.getById(boardCopyrightOwnerId).getName());
        }
        String exhibitionBoardCatId = exhibitionBoard.getExhibitionBoardCatId();
        if (exhibitionBoardCatId != null) {
            exhibitionBoard.setExhibitionBoardCatName(this.exhibitionBoardCatService.getById(exhibitionBoardCatId).getName());
        }

        if (StringUtils.isNotEmpty(exhibitionBoard.getVideoContentCopyrightOwnerId())) {
            String name = this.copyrightOwnerService.getById(exhibitionBoard.getVideoContentCopyrightOwnerId()).getName();
            exhibitionBoard.setVideoContentCopyrightOwnerName(name);
        }
        if (exhibitionBoard.getVideoContentId() != null) {
            String thumbnail = this.videoContentService.getById(exhibitionBoard.getVideoContentId()).getThumbnail();
            exhibitionBoard.setBoardVideoContentThumbnail(thumbnail);
        }

        LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, id);
        assetQueryWrapper.eq(Asset::getPublished, true);
        assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_AUDIO.name());
        if (audioLanguage != null) {
            assetQueryWrapper.eq(Asset::getLanguage, audioLanguage.name());
        }
        final List<Asset> audioList = this.assetService.list(assetQueryWrapper);
        List<String> languageList = audioList.stream().map(Asset::getLanguage).collect(Collectors.toList());
        List<Map<String, String>> audioLanguageList = new ArrayList<>();
        for (LanguageEnum languageEnum : LanguageEnum.values()) {
            if (languageList.contains(languageEnum.name())) {
                Map<String, String> map = new LinkedHashMap<>(2);
                map.put("code", languageEnum.name());
                map.put("name", languageEnum.getName());
                audioLanguageList.add(map);
            }
        }
        exhibitionBoard.setAudioList(audioList);
        exhibitionBoard.setAudioLanguageList(audioLanguageList);

        assetQueryWrapper.clear();
        assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, id);
        assetQueryWrapper.eq(Asset::getPublished, true);
        assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_DATUM.name());
        final List<Asset> datumList = this.assetService.list(assetQueryWrapper);
        exhibitionBoard.setDatumList(datumList);

        final String videoContentId = exhibitionBoard.getVideoContentId();
        if (videoContentId != null) {
            final VideoContent videoContent = this.videoContentService.getById(videoContentId);
            exhibitionBoard.setVideoContentName(videoContent.getName());

            assetQueryWrapper.clear();
            assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, videoContentId);
            assetQueryWrapper.eq(Asset::getPublished, true);
            assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.VIDEO_CONTENT.name());
            if (videoLanguage != null) {
                assetQueryWrapper.eq(Asset::getLanguage, videoLanguage.name());
            }
            final List<Asset> videoList = this.assetService.list(assetQueryWrapper);
            languageList.clear();
            languageList = videoList.stream().map(Asset::getLanguage).collect(Collectors.toList());
            List<Map<String, String>> videoLanguageList = new ArrayList<>();
            for (LanguageEnum languageEnum : LanguageEnum.values()) {
                if (languageList.contains(languageEnum.name())) {
                    Map<String, String> map = new LinkedHashMap<>(2);
                    map.put("code", languageEnum.name());
                    map.put("name", languageEnum.getName());
                    videoLanguageList.add(map);
                }
            }
            exhibitionBoard.setVideoList(videoList);
            exhibitionBoard.setVideoLanguageList(videoLanguageList);
        }
        // 填充编号
        setBoardSerial(exhibitionBoard);
        return getResult(exhibitionBoard);
    }

    @PostMapping("/equitment/playLog")
    @RequiresAuthentication  //@RequiresPermissions("t:board:statistic:statisticBoardInfo")
    @ApiOperation(value = "播放记录信息反馈", notes = "播放记录信息反馈")
    public Map<String, Object> boardStatisticInfo(@Validated(value = {Add.class}) TBoardStatistic tBoardStatistic) {
        // 展板信息统计
        try {
            Object result = tBoardStatisticService.boardStatisticInfo(tBoardStatistic, false);
            // 返回操作结果
            if (result != null && (boolean) result) {
                return getSuccessResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 保存失败
        return getFailResult();
    }

    @Scheduled(cron = "0 0/5 * * * *")
    public void boardStatistic() {
        try {
            System.out.println("定时汇总播放记录开始...");
            tBoardStatisticService.boardStatisticInfo(null, true);
            System.out.println("定时汇总播放记录成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @PostMapping("/learningProject/getPage")
    @RequiresAuthentication  //@RequiresPermissions("learning:project:page")
    @ApiOperation(value = "查询学习项目", notes = "查询学习项目")
    public Map<String, Object> getLearningProjectPageList(GenericPageParam genericPageParam) {
        LambdaQueryWrapper<LearningProject> queryWrapper = new LambdaQueryWrapper<>();
        // 对名称或编码模糊查询
        if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
            queryWrapper.like(LearningProject::getName, genericPageParam.getNameOrCode());
        }
        // 根据创建时间区间检索
        if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
            queryWrapper.ge(LearningProject::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                    .le(LearningProject::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
        }
        // 设置排序规则
        queryWrapper.orderByDesc(LearningProject::getCreateTime);
        // 设置查询内容
        queryWrapper.select(
                LearningProject::getId,
                LearningProject::getName,
                LearningProject::getRemarks,
                LearningProject::getCreateTime,
                LearningProject::getUpdateTime);

        Page<LearningProject> page = this.learningProjectService.page(getPage(), queryWrapper);
        for (LearningProject learningProject : page.getRecords()) {
            LambdaQueryWrapper<LearningContent> lambdaQueryWrapper = Wrappers.<LearningContent>lambdaQuery()
                    .eq(LearningContent::getLearningProjectId, learningProject.getId())
                    .eq(LearningContent::getPublished, true).orderByDesc(LearningContent::getSortorder);
            lambdaQueryWrapper.select(LearningContent::getName, LearningContent::getCover);
//            List<LearningContent> learningContentList = this.learningContentService.list(lambdaQueryWrapper);
//            if (!learningContentList.isEmpty()) {
//                String learningContentNames = learningContentList.stream().map(LearningContent::getName).collect(Collectors.joining("、"));
//                learningProject.setThumbnail(learningContentList.get(0).getCover());
//                learningProject.setLearningContentNames(learningContentNames);
//            }

            //学习项目图片默认展示主学习内容图片
            List<LearningContent> list1 = learningContentService.list(new LambdaQueryWrapper<LearningContent>().eq(LearningContent::getLearningProjectId, learningProject.getId())
                    .eq(LearningContent::getIsMajor, true));
            if (list1.size() > 0) {
                learningProject.setThumbnail(list1.get(0).getCover());
            }

            List<ExhibitionBoard> list = exhibitionBoardService.getList(learningProject.getId(), true);
            if (!list.isEmpty()) {
                String exhibitionBoardNames = list.stream().map(ExhibitionBoard::getName).collect(Collectors.joining("、"));
                //所有展板名称
                learningProject.setExhibitionBoardNames(exhibitionBoardNames);
            }

        }
        return getResult(page);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "learningProjectId", value = "学习项目ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String")
    })
    @PostMapping("/learningContent/getPage")
    @RequiresAuthentication  //@RequiresPermissions("learning:content:page")
    @ApiOperation(value = "查询学习内容", notes = "查询学习内容")
    public Map<String, Object> getLearningContentPageList(GenericPageParam genericPageParam,
                                                          @RequestParam(value = "learningProjectId", required = false) String learningProjectId) {
        TUser tUser = getcurUser();
        LambdaQueryWrapper<LearningContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LearningContent::getPublished, true);
        //只查询子学习项目
        queryWrapper.eq(LearningContent::getIsMajor, false);
        // 对名称或编码模糊查询
        if (StringUtils.isNotBlank(learningProjectId)) {
            queryWrapper.eq(LearningContent::getLearningProjectId, learningProjectId);
        }
        // 对名称或编码模糊查询
        if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
            queryWrapper.like(LearningContent::getName, genericPageParam.getNameOrCode());
        }
        // 根据创建时间区间检索
        if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
            queryWrapper.ge(LearningContent::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                    .le(LearningContent::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
        }
        //学习内容适用范围筛选条件
        queryWrapper.apply(" (applicable_scope = 'ALL_PLAT' or (applicable_scope = 'THIS_ORGAN' and organ_code = '" + tUser.getOrgCode() +
                "') or (applicable_scope = 'THIS_ORGAN_SUB' and  organ_code like '" + tUser.getOrgCode() + "%' ))");
//        queryWrapper.and(wapper->wapper.eq(LearningContent::getApplicableScope,"ALL_PLAT")
//                .or(wapper2->wapper2.eq(LearningContent::getApplicableScope,"THIS_ORGAN").eq(LearningContent::getOrganCode,tUser.getOrgCode()))
//                .or(wapper2->wapper2.eq(LearningContent::getApplicableScope,"THIS_ORGAN_SUB").likeRight(LearningContent::getOrganCode,tUser.getOrgCode())));
        // 设置排序规则
        queryWrapper.orderByDesc(LearningContent::getSortorder);
        // 设置查询内容
        queryWrapper.select(
                LearningContent::getId,
                LearningContent::getName,
                LearningContent::getCover,
                LearningContent::getSortorder,
                LearningContent::getCreateTime,
                LearningContent::getUpdateTime);
        Page<LearningContent> page = this.learningContentService.page(getPage(), queryWrapper);
        for (LearningContent learningContent : page.getRecords()) {
            LambdaQueryWrapper<LearningContentBoard> lambdaQueryWrapper = Wrappers.<LearningContentBoard>lambdaQuery().eq(LearningContentBoard::getLearningContentId, learningContent.getId());
            int exhibitionBoardCount = this.learningContentBoardService.count(lambdaQueryWrapper);
            learningContent.setExhibitionBoardCount(exhibitionBoardCount);
        }
        return getResult(page);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "boardId", value = "展板内容ID", paramType = "path", dataType = "String", required = true)
    })
    @GetMapping("/exhibitionBoard/getRefMaterial/{boardId}")
    @RequiresAuthentication  //@RequiresPermissions("learning:project:page")
    @ApiOperation(value = "展板参考资料分页查询", notes = "展板参考资料分页查询")
    public Map<String, Object> getBoardRefMaterial(@PathVariable(value = "boardId") String boardId) {
        final ExhibitionBoard exhibitionBoard = this.exhibitionBoardService.getById(boardId);
        final LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoard.getId());
        assetQueryWrapper.eq(Asset::getPublished, true);
        assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_DATUM.name());
        final Page<Asset> datumList = this.assetService.page(this.getPage(), assetQueryWrapper);
        return getResult(datumList);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "learningProjectId", value = "学习项目ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String")
    })
    @PostMapping("/exhibitionBoard/search")
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:page")
    @ApiOperation(value = "模糊搜索查询", notes = "模糊搜索查询")
    public Map<String, Object> getLearningContentBoardPageList(GenericPageParam genericPageParam,
                                                               @RequestParam(value = "learningProjectId", required = false) String learningProjectId) {
        IPage<ExhibitionBoard> page = this.learningContentBoardService.getBoardPageByLearningProjectId(getPage(), learningProjectId, genericPageParam.getNameOrCode());
        return getResult(page);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "learningContentId", value = "学习内容ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String")
    })
    @PostMapping("/exhibitionBoard/getByContentSearch")
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:page")
    @ApiOperation(value = "根据学习内容模糊搜索查询", notes = "根据学习内容模糊搜索查询")
    public Map<String, Object> getByContentSearch(GenericPageParam genericPageParam,
                                                  @RequestParam(value = "learningContentId", required = false) String learningContentId) {
        IPage<ExhibitionBoard> page = this.learningContentBoardService.getBoardPageByLearningContentId(getPage(), learningContentId, genericPageParam.getNameOrCode());
        return getResult(page);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer")
    })
    @PostMapping("/asset/search")
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:page")
    @ApiOperation(value = "获取当前用户可查看的视频信息", notes = "获取当前用户可查看的视频信息")
    public Map<String, Object> getAssetPageByOrganId() {
        final TUser tUser = getcurUser();
        if (tUser != null && tUser.getOrgCode() != null) {
            final String orgCode = tUser.getOrgCode();
            IPage<Asset> page = this.learningContentBoardService.getAssetPageByOrganCode(getPage(), orgCode);
            return getResult(page);
        }
        return getFailResult("1109", "账号认证失败");
    }

    @ApiOperation(value = "app最新版本查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionNo", value = "app版本号", dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/version/check")
    @RequiresAuthentication  //@RequiresPermissions("app:version:check")
    public Map<String, Object> versionCheck(String versionNo) {
        TAppVersion current = new TAppVersion();//当前版本
        // 查询最新版本号信息
        QueryWrapper<TAppVersion> qw = new QueryWrapper<>();
        qw.eq("is_current", 1);
        qw.select().orderByDesc("update_time");
        List<TAppVersion> currentList = appVersionService.list(qw);
        if (currentList == null || currentList.size() == 0) {
            return getFailResult("未设置当前版本信息");
        } else {
            current = currentList.get(0);
        }
        Map map = new HashMap();

        if (StringUtils.isNotBlank(versionNo)) {
            final String[] versionNoArray = StringUtils.split(versionNo, ".");
            final String[] currentVersionNoArray = StringUtils.split(current.getAppVersion(), ".");
            if (Integer.parseInt(versionNoArray[0]) >= Integer.parseInt(currentVersionNoArray[0])) {
                if (Integer.parseInt(versionNoArray[1]) >= Integer.parseInt(currentVersionNoArray[1])) {
                    if (Integer.parseInt(versionNoArray[2]) >= Integer.parseInt(currentVersionNoArray[2])) {
                        map.put("isLatest", true);
                    } else {
                        map.put("isLatest", false);
                    }
                } else {
                    map.put("isLatest", false);
                }
            } else {
                map.put("isLatest", false);
            }
        } else {
            map.put("isLatest", false);
        }
        map.put("versionNo", current.getAppVersion());
        map.put("versionUrl", current.getApkUrl());
        map.put("updateLog", current.getUpdateLog());
        map.put("updateTime", current.getUpdateTime());
        return getResult(map);
    }

    @ApiOperation(value = "app界面图片查询")
    @GetMapping(value = "/app/picSelect")
    //@RequiresAuthentication  //@RequiresPermissions("app:pic:select")
    public Map<String, Object> appPicSelect() {
        TAppDirPic currentDir = null;// 当前目录页面
        TAppRunPic currentRun = null;// 当前运行画面
        // 查询当前目录界面信息
        QueryWrapper<TAppDirPic> qw = new QueryWrapper<>();
        qw.eq("is_current", 1);
        qw.select().orderByDesc("create_time");
        List<TAppDirPic> currentDirList = appDirPicService.list(qw);
        if (currentDirList != null && currentDirList.size() > 0) {
            currentDir = currentDirList.get(0);
        }
        QueryWrapper<TAppRunPic> qw1 = new QueryWrapper<>();
        qw1.eq("is_current", 1);
        qw1.select().orderByDesc("create_time");
        List<TAppRunPic> currentRunList = appRunPicService.list(qw1);
        if (currentRunList != null && currentRunList.size() > 0) {
            currentRun = currentRunList.get(0);
        }

        Map map = new HashMap();
        map.put("dirImgUrl", currentDir != null ? currentDir.getAppDirPic() : "未设置目录图片");
        map.put("runImgUrl", currentRun != null ? currentRun.getAppRunPic() : "未设置运行图片");
        return getResult(map);
    }

    /**
     * 填充编号
     */
    public void setBoardSerial(ExhibitionBoard board) {
        String serialNo = board.getSerialNo();
        int length = serialNo.length();
        for (int i = 0; i < 3 - length; i++) {
            board.setSerialNo("0" + serialNo);
            serialNo = board.getSerialNo();
        }
    }
}