package com.yunzhi.marketing.service.impl;

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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.entity.TaMiniappOrgIcon;
import com.yunzhi.marketing.entity.TaMiniappTheme;
import com.yunzhi.marketing.mapper.TaMiniappIconMapper;
import com.yunzhi.marketing.mapper.TaMiniappOrgIconMapper;
import com.yunzhi.marketing.mapper.TaMiniappThemeMapper;
import com.yunzhi.marketing.service.ITaMiniappThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class TaMiniappThemeServiceImpl extends ServiceImpl<TaMiniappThemeMapper, TaMiniappTheme> implements ITaMiniappThemeService {
    @Autowired
    TaMiniappThemeMapper taMiniappThemeMapper;

    @Autowired
    TaMiniappIconMapper taMiniappIconMapper;

    @Autowired
    TaMiniappOrgIconMapper taMiniappOrgIconMapper;

    @Override
    public IPage<TaMiniappTheme> getThemeList(Integer orgId, Boolean includeNotUsed, Integer pageNum, Integer pageSize) {
        QueryWrapper<TaMiniappTheme> queryTheme = new QueryWrapper<>();
        queryTheme.eq("org_id", orgId);

        if (includeNotUsed) {
            queryTheme.gt("status", CommConstant.STATUS_DELETE);
        } else {
            queryTheme.eq("status", CommConstant.STATUS_NORMAL);
        }

        IPage<TaMiniappTheme> result = this.page(new Page<>(pageNum, pageSize), queryTheme);
        if (result.getSize() > 0) {
            for (TaMiniappTheme theme: result.getRecords()) {
                theme.setIconList(taMiniappIconMapper.getListByTheme(theme.getThemeId()));
            }
        }

        return result;
    }

    @Override
    public ResponseBean editTheme(TaMiniappTheme taMiniappTheme) {
        if (taMiniappTheme == null) {
            return ResponseBean.error("没有需要保存的内容", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (StringUtils.isEmpty(taMiniappTheme.getThemeTitle())) {
            return ResponseBean.error("主题名称不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (StringUtils.isEmpty(taMiniappTheme.getNaviFrontColor()) || StringUtils.isEmpty(taMiniappTheme.getNaviBackColor())) {
            return ResponseBean.error("导航栏前景或者背景色不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (StringUtils.isEmpty(taMiniappTheme.getTabbarFrontColor()) || StringUtils.isEmpty(taMiniappTheme.getTabbarBackColor())) {
            return ResponseBean.error("TabBar前景或者背景色不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (StringUtils.isEmpty(taMiniappTheme.getTabbarSelectColor())) {
            return ResponseBean.error("TabBar选择色不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (taMiniappTheme.getStatus() == null) {
            taMiniappTheme.setStatus(CommConstant.STATUS_NORMAL);
        }

        Integer themeId = taMiniappTheme.getThemeId();
        if (themeId != null && themeId > 0) {
            // 先删除所有图标
            QueryWrapper<TaMiniappOrgIcon> wrapper = new QueryWrapper<>();
            wrapper.eq("org_id", taMiniappTheme.getOrgId());
            wrapper.eq("theme_id", themeId);

            taMiniappOrgIconMapper.delete(wrapper);
        }

        // 先主表
        if (themeId != null && themeId > 0) {
            if (!this.updateById(taMiniappTheme)) {
                return ResponseBean.error("更新主题内容失败", ResponseBean.ERROR_UNAVAILABLE);
            }
        } else {
            if (!this.save(taMiniappTheme)) {
                return ResponseBean.error("保存主题内容失败", ResponseBean.ERROR_UNAVAILABLE);
            }
        }

        // 再子表
        List<TaMiniappOrgIcon> iconList = taMiniappTheme.getIconList();
        Integer orgId = taMiniappTheme.getOrgId();
        if (iconList != null) {
            Integer size = iconList.size();
            LocalDateTime now = LocalDateTime.now();
            Integer successd = 0;

            for (int i = size - 1; i >= 0; i --) {
                TaMiniappOrgIcon icon = iconList.get(i);
                icon.setOrgId(orgId);
                icon.setThemeId(themeId);
                icon.setStatus(CommConstant.STATUS_NORMAL);
                icon.setSort(i + 1);
                icon.setCreateDate(now);
                icon.setUpdateDate(now);
                successd += taMiniappOrgIconMapper.insert(icon);
            }

            if (!size.equals(successd)) {
                return ResponseBean.error("保存主题图标内容失败", ResponseBean.ERROR_UNAVAILABLE);
            }
        }

        return ResponseBean.success(taMiniappTheme);
    }

    @Override
    public ResponseBean deleteTheme(TaMiniappTheme taMiniappTheme) {
        LocalDateTime now = LocalDateTime.now();

        // 先处理子表
        TaMiniappOrgIcon icon = new TaMiniappOrgIcon();
        icon.setStatus(CommConstant.STATUS_DELETE);
        icon.setUpdateDate(now);
        icon.setOrgId(taMiniappTheme.getOrgId());
        icon.setThemeId(taMiniappTheme.getThemeId());

        UpdateWrapper<TaMiniappOrgIcon> wrapper = new UpdateWrapper<>();
        wrapper.set("status", CommConstant.STATUS_DELETE);
        wrapper.set("update_date", LocalDateTime.now());
        wrapper.eq("org_id", taMiniappTheme.getOrgId());
        wrapper.eq("theme_id", taMiniappTheme.getThemeId());

        taMiniappOrgIconMapper.update(icon, wrapper);

        // 再处理主表
        taMiniappTheme.setStatus(CommConstant.STATUS_DELETE);
        taMiniappTheme.setModifyDate(now);
//        taMiniappTheme.setModifyUser();

        if (this.updateById(taMiniappTheme)) {
            return ResponseBean.success("");
        } else {
            return ResponseBean.error("删除主题内容失败", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    @Override
    public TaMiniappTheme getProfileById(Integer id) {
        if (id == null || id < 1) return null;

        TaMiniappTheme taMiniappTheme = this.getById(id);
        if (taMiniappTheme.getStatus().equals(CommConstant.STATUS_DELETE)) {
            return null;
        }

        QueryWrapper<TaMiniappOrgIcon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", taMiniappTheme.getOrgId());
        queryWrapper.eq("theme_id", taMiniappTheme.getThemeId());
        queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
        queryWrapper.orderByDesc("sort");

        taMiniappTheme.setIconList(taMiniappOrgIconMapper.selectList(queryWrapper));

        return taMiniappTheme;
    }
}
