package com.gree.mall.manager.logic.marketing;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.MarketingMaterials;
import com.gree.mall.manager.plus.entity.MarketingMaterialsGroup;
import com.gree.mall.manager.plus.entity.MarketingMaterialsTag;
import com.gree.mall.manager.plus.service.MarketingMaterialsGroupService;
import com.gree.mall.manager.plus.service.MarketingMaterialsService;
import com.gree.mall.manager.plus.service.MarketingMaterialsTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销素材分组
 */
@Service
public class MarketingMaterialsGroupLogic {

    @Autowired
    private MarketingMaterialsGroupService marketingMaterialsGroupService;
    @Autowired
    private MarketingMaterialsService marketingMaterialsService;
    @Autowired
    private MarketingMaterialsTagService marketingMaterialsTagService;
    @Autowired
    private CommonLogic commonLogic;


    /**
     * 获取分组
     */
    public List<MarketingMaterialsGroup> list(HttpServletRequest request, String groupName) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        return marketingMaterialsGroupService.lambdaQuery()
                .eq(StringUtils.isNotBlank(groupName), MarketingMaterialsGroup::getGroupName, groupName)
                .in(CollectionUtils.isNotEmpty(companyWechatIds), MarketingMaterialsGroup::getCompanyWechatId, companyWechatIds)
                .orderByDesc(MarketingMaterialsGroup::getCreateTime)
                .list();
    }

    /**
     * 添加分组
     */
    public MarketingMaterialsGroup add(HttpServletRequest request, String groupName) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        String loginCompanyWechatId = adminUser.getCompanyWechatId();
        if (StringUtils.isBlank(loginCompanyWechatId)) {
            throw new RemoteServiceException("运营账号暂无权限操作");
        }

        MarketingMaterialsGroup marketingMaterialsGroup = new MarketingMaterialsGroup();
        marketingMaterialsGroup.setGroupName(groupName);
        marketingMaterialsGroup.setCreateTime(new Date());
        marketingMaterialsGroup.setCreateBy(adminUser.getAdminUserId());
        marketingMaterialsGroup.setCompanyWechatId(loginCompanyWechatId);
        marketingMaterialsGroup.setCompanyName(adminUser.getCompanyWechatId());

        marketingMaterialsGroupService.save(marketingMaterialsGroup);
        return marketingMaterialsGroup;

    }

    /**
     * 修改分组
     */
    public void update(String groupId, String groupName) {

        MarketingMaterialsGroup marketingMaterialsGroup = new MarketingMaterialsGroup();
        marketingMaterialsGroup.setGroupName(groupName);
        marketingMaterialsGroup.setMarketingGroupId(groupId);
        marketingMaterialsGroupService.updateById(marketingMaterialsGroup);

    }

    /**
     * 删除分组
     */
    @Transactional
    public void delete(String groupId) {

        //删除分组
        marketingMaterialsGroupService.lambdaUpdate()
                .eq(MarketingMaterialsGroup::getMarketingGroupId, groupId)
                .remove();

        //删除与素材绑定的tage
        List<MarketingMaterials> list = marketingMaterialsService.lambdaQuery()
                .select(MarketingMaterials::getMarketingMaterialsId)
                .eq(MarketingMaterials::getMarketingGroupId, groupId)
                .list();
        List<String> getMarketingMaterialsIds = list.stream().map(MarketingMaterials::getMarketingMaterialsId).collect(Collectors.toList());

        if(CollectionUtils.isNotEmpty(getMarketingMaterialsIds)){
            marketingMaterialsTagService.lambdaUpdate()
                    .in(MarketingMaterialsTag::getMarketingMaterialsId,getMarketingMaterialsIds)
                    .remove();
        }


        //删除该分组底下的所有的营销素材
        marketingMaterialsService.lambdaUpdate()
                .eq(MarketingMaterials::getMarketingGroupId, groupId)
                .remove();


    }

}
