package com.yida.data.school.program.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.program.EduSmallProgram;
import com.yida.data.common.core.entity.program.YiDaSmallProgramVO;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.EduYidaApp;
import com.yida.data.common.core.utils.AppUtil;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.dto.program.SmallProgramSaveDTO;
import com.yida.data.school.dto.program.SmallProgramSelectPageDTO;
import com.yida.data.school.program.mapper.EduSmallProgramMapper;
import com.yida.data.school.program.service.EduSmallProgramService;
import com.yida.data.school.vo.program.ListSmallProgramDeptVO;
import com.yida.data.school.vo.program.ProgramConfigDTO;
import com.yida.data.school.vo.program.SmallProgramInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 小程序表 Service实现
 *
 * @author ZYJ
 * @date 2021-12-10 13:44:31
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSmallProgramServiceImpl extends ServiceImpl
        <EduSmallProgramMapper, EduSmallProgram> implements EduSmallProgramService {

    private final CommonService commonService;

    /**
     * 查询小程序列表数据
     *
     * @param smallProgramSelectPageDTO 小程序后台列表请求类
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yida.data.school.vo.program.SmallProgramSelectPageVO>
     * @author ZYJ
     * @date 2021/12/10 14:04
     */
    @Override
    public IPage<EduSmallProgram> listSmallProgramPage(SmallProgramSelectPageDTO smallProgramSelectPageDTO,CurrentUser currentUser) {
        // 登陆用户信息
        if (Objects.nonNull(currentUser.getMainDeptId())) {
            smallProgramSelectPageDTO.setDeptId(currentUser.getMainDeptId());
        } else {
            smallProgramSelectPageDTO.setDeptId(FebsUtil.getTopDeptId());
        }
        return this.baseMapper.listSmallProgramPage(smallProgramSelectPageDTO.toPage(), smallProgramSelectPageDTO);
    }

    /**
     * 根据小程序id查询小程序详情
     *
     * @param smallProgramId 小程序id
     * @return com.yida.data.school.vo.program.SmallProgramInfoVO
     * @author ZYJ
     * @date 2021/12/10 14:32
     */
    @Override
    public SmallProgramInfoVO getSmallProgramInfo(Long smallProgramId) {
        EduSmallProgram eduSmallProgram = this.getById(smallProgramId);
        SmallProgramInfoVO smallProgramInfoVO = new SmallProgramInfoVO();
        BeanUtils.copyProperties(eduSmallProgram, smallProgramInfoVO);
        return smallProgramInfoVO;
    }

    /**
     * 保存小程序数据
     *
     * @param smallProgramSaveDTO 保存小程序请求类
     * @author ZYJ
     * @date 2021/12/10 15:19
     */
    @Override
    public void saveSmallProgram(SmallProgramSaveDTO smallProgramSaveDTO,CurrentUser currentUser) {
        EduSmallProgram eduSmallProgram = new EduSmallProgram();
        BeanUtils.copyProperties(smallProgramSaveDTO, eduSmallProgram);

        // 登陆用户信息
        if (Objects.nonNull(currentUser.getMainDeptId())) {
            eduSmallProgram.setDeptId(currentUser.getMainDeptId());
        } else {
            eduSmallProgram.setDeptId(FebsUtil.getTopDeptId());
        }
//         查询易达两微一端配置数据
        EduYidaApp yidaApp = commonService.getYidaApp(eduSmallProgram.getDeptId());
        if (Objects.nonNull(yidaApp)) {
            if (Objects.isNull(eduSmallProgram.getId())) {
                // 新增
                eduSmallProgram.setAreaId(yidaApp.getAppAreaId());
                YiDaSmallProgramVO yiDaSmallProgramVO = new YiDaSmallProgramVO();
                yiDaSmallProgramVO.setAuthorityType(2);
                yiDaSmallProgramVO.setAreaId(eduSmallProgram.getAreaId());
                yiDaSmallProgramVO.setH5Url(eduSmallProgram.getLinkUrl());
                yiDaSmallProgramVO.setIconUrl(eduSmallProgram.getCoverLogoImg());
                yiDaSmallProgramVO.setIsShowName(eduSmallProgram.getLogoType());
                yiDaSmallProgramVO.setKeyWords(eduSmallProgram.getKeyWords());
                yiDaSmallProgramVO.setShareTitle(eduSmallProgram.getShareTitle());
                yiDaSmallProgramVO.setShareUrl(eduSmallProgram.getShareUrl());
                yiDaSmallProgramVO.setTypeName(eduSmallProgram.getShareTitle());
                yiDaSmallProgramVO.setSmallProgramTypeId("1");
                yiDaSmallProgramVO.setAuthorityType(2);
                yiDaSmallProgramVO.setTitle(eduSmallProgram.getSmallProgramName());
                eduSmallProgram.setAppSmallProgramId(AppUtil.smallProgramSave(yiDaSmallProgramVO, yidaApp,eduSmallProgram.getShareImg()));
            } else {
                // 编辑
                eduSmallProgram.setAreaId(yidaApp.getAppAreaId());
                eduSmallProgram.setUpdateName(currentUser.getUsername());
                eduSmallProgram.setAppSmallProgramId(this.getById(eduSmallProgram.getId()).getAppSmallProgramId());
                YiDaSmallProgramVO yiDaSmallProgramVO = new YiDaSmallProgramVO();
                yiDaSmallProgramVO.setId(eduSmallProgram.getAppSmallProgramId());
                yiDaSmallProgramVO.setAuthorityType(2);
                yiDaSmallProgramVO.setAreaId(eduSmallProgram.getAreaId());
                yiDaSmallProgramVO.setH5Url(eduSmallProgram.getLinkUrl());
                yiDaSmallProgramVO.setIconUrl(eduSmallProgram.getCoverLogoImg());
                yiDaSmallProgramVO.setIsShowName(eduSmallProgram.getLogoType());
                yiDaSmallProgramVO.setKeyWords(eduSmallProgram.getKeyWords());
                yiDaSmallProgramVO.setShareTitle(eduSmallProgram.getSmallProgramName());
                yiDaSmallProgramVO.setShareUrl(eduSmallProgram.getShareUrl());
                yiDaSmallProgramVO.setShareTitle(eduSmallProgram.getShareTitle());
                yiDaSmallProgramVO.setTypeName(eduSmallProgram.getShareTitle());
                yiDaSmallProgramVO.setSmallProgramTypeId("1");
                yiDaSmallProgramVO.setTitle(eduSmallProgram.getSmallProgramName());
                eduSmallProgram.setAppSmallProgramId(AppUtil.smallProgramSave(yiDaSmallProgramVO, yidaApp,eduSmallProgram.getShareImg()));
            }
        }
        if (ObjectUtil.isEmpty(eduSmallProgram.getShareTitle())){
            eduSmallProgram.setShareTitle(eduSmallProgram.getSmallProgramName());
        }
        if (ObjectUtil.isEmpty(eduSmallProgram.getShareUrl())){
            eduSmallProgram.setSmallProgramName(eduSmallProgram.getLinkUrl());
        }
        if (ObjectUtil.isEmpty(eduSmallProgram.getShareImg())){
            eduSmallProgram.setShareTitle(eduSmallProgram.getCoverLogoImg());

        }
        this.saveOrUpdate(eduSmallProgram);
    }

    /**
     * 批量删除小程序数据
     * @param smallProgramIds 小程序id集合
     * @author ZYJ
     * @date 2021/12/10 17:50
     */
    @Override
    public void deleteSmallProgramBatch(List<Long> smallProgramIds,CurrentUser currentUser) {
        Long deptId;
        // 登陆用户信息
        if (Objects.nonNull(currentUser.getMainDeptId())) {
            deptId = currentUser.getMainDeptId();
        } else {
            deptId = FebsUtil.getTopDeptId();
        }
        // 查询易达两微一端配置数据
        EduYidaApp yidaApp = commonService.getYidaApp(deptId);

        smallProgramIds.forEach(smallProgramId -> {
            EduSmallProgram smallProgram = this.getById(smallProgramId);
            // 删除本地数据
            this.removeById(smallProgramId);
            // 删除两微一端数据
            if (Objects.nonNull(smallProgram) && Objects.nonNull(yidaApp)) {
                AppUtil.smallProgramDelete(yidaApp, smallProgram.getAppSmallProgramId());
            }
        });
    }

    @Override
    public List<ListSmallProgramDeptVO> listSmallProgramDept() {

        return baseMapper.listSmallProgramDeptVO();
    }

    @Override
    public void setProgramConfig(ProgramConfigDTO config,CurrentUser currentUser) {
        Long deptId;
        // 登陆用户信息
        if (Objects.nonNull(currentUser.getMainDeptId())) {
            deptId = currentUser.getMainDeptId();
        } else {
            deptId = FebsUtil.getTopDeptId();
        }
        // 查询易达两微一端配置数据
        Dept dept = commonService.getDept(deptId);
        EduYidaApp yidaApp = commonService.getYidaApp(deptId);
        String appColId = dept.getYidaAppColId();
        EduSmallProgram one = getOne(Wrappers.lambdaQuery(new EduSmallProgram())
                .eq(EduSmallProgram::getAppSmallProgramId, config.getId()));
        one.setSortNum(config.getSort());
        one.setShowPosition(config.getIsPosition());
        updateById(one);
//        Map<String, Object> paramMap = Convert.toMap(String.class, Object.class, config);
//        AppUtil.columnSmallProgramSave(yidaApp,appColId,paramMap);
    }
}
