package com.storlead.crm.marketing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.crm.marketing.dto.MarketingCampaignDTO;
import com.storlead.crm.marketing.entity.CustomerBaseEntity;
import com.storlead.crm.marketing.entity.MarketingCampaignEntity;
import com.storlead.crm.marketing.mapper.MarketingCampaignEntityMapper;
import com.storlead.crm.marketing.service.CustomerBaseEntityService;
import com.storlead.crm.marketing.service.MarketingCampaignEntityService;
import com.storlead.crm.marketing.vo.MarketingCampaignVO;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MarketingCampaignEntityServiceImpl
        extends MyBaseServiceImpl<MarketingCampaignEntityMapper, MarketingCampaignEntity>
        implements MarketingCampaignEntityService {

    @Resource
    private CustomerBaseEntityService customerBaseEntityService;
    @Override
    public Result<Object> getList(MarketingCampaignDTO marketingCampaignDTO) {
        IPage<MarketingCampaignVO> pageVO = new Page<>(marketingCampaignDTO.getPageIndex(), marketingCampaignDTO.getPageSize());
        IPage<MarketingCampaignEntity> page = new Page<>(marketingCampaignDTO.getPageIndex(), marketingCampaignDTO.getPageSize());
        LambdaQueryWrapper<MarketingCampaignEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MarketingCampaignEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        IPage<MarketingCampaignEntity> pageList = this.page(page, queryWrapper);

//        List<MarketingCampaignEntity> list = this.list(queryWrapper);
        pageVO = SysBaseField.page(pageList, MarketingCampaignVO.class);
        if (CollectionUtils.isEmpty(pageList.getRecords())){
            return Result.ok(pageVO);
        }
        List<MarketingCampaignVO> marketingCampaignVOList = pageVO.getRecords();
        List<Long> marketingCampaignIdList = marketingCampaignVOList.stream().map(MarketingCampaignVO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<CustomerBaseEntity> customerBaseEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerBaseEntityLambdaQueryWrapper.in(CustomerBaseEntity::getMarketingCampaignId, marketingCampaignIdList);
        customerBaseEntityLambdaQueryWrapper.eq(CustomerBaseEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<CustomerBaseEntity> customerBaseEntityList = customerBaseEntityService.list(customerBaseEntityLambdaQueryWrapper);
        //获取全部的关联客户id
        List<Long> customerIdList = customerBaseEntityList.stream().map(CustomerBaseEntity::getCustomerId).collect(Collectors.toList());
//        LambdaQueryWrapper<CustomerEntity> customerBaseEntityQueryWrapper = new LambdaQueryWrapper<>();


        return Result.ok(pageVO);
    }

    @Override
    public Result<Object> addMarket(MarketingCampaignDTO marketingCampaignDTO) {
        MarketingCampaignEntity marketingCampaignEntity = new MarketingCampaignEntity();
        BeanUtils.copyProperties(marketingCampaignDTO, marketingCampaignEntity);
        boolean isOk = this.save(marketingCampaignEntity);
        if (!isOk) {

            return Result.error("添加失败");
        }
        //添加关联客户
        List<Long> customerIdList = marketingCampaignDTO.getCustomerIdList();
        if (!CollectionUtils.isEmpty(customerIdList)) {
            List<CustomerBaseEntity> customerBaseEntityList = new ArrayList<>();
            for (Long customerId : customerIdList) {
                CustomerBaseEntity customerBaseEntity = new CustomerBaseEntity();
                customerBaseEntity.setCustomerId(customerId);
                customerBaseEntity.setMarketingCampaignId(marketingCampaignEntity.getId());
                customerBaseEntityList.add(customerBaseEntity);
            }
            boolean saveOk =customerBaseEntityService.saveBatch(customerBaseEntityList);
            if (!saveOk){
                return Result.error("添加失败");
            }
        }
        return Result.ok();
    }

    @Override
    public Result<Object> editMarket(MarketingCampaignDTO marketingCampaignDTO) {
        MarketingCampaignEntity marketingCampaignEntity = new MarketingCampaignEntity();
        BeanUtils.copyProperties(marketingCampaignDTO, marketingCampaignEntity);
        boolean isOk = this.updateById(marketingCampaignEntity);
        if (!isOk) {
            return Result.error("修改失败");
        }
        //添加关联客户
        List<Long> customerIdList = marketingCampaignDTO.getCustomerIdList();
        LambdaUpdateWrapper<CustomerBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CustomerBaseEntity::getMarketingCampaignId, marketingCampaignEntity.getId());
        customerBaseEntityService.remove(updateWrapper);
        if (!CollectionUtils.isEmpty(customerIdList)) {
            List<CustomerBaseEntity> customerBaseEntityList = new ArrayList<>();
            for (Long customerId : customerIdList) {
                CustomerBaseEntity customerBaseEntity = new CustomerBaseEntity();
                customerBaseEntity.setCustomerId(customerId);
                customerBaseEntity.setMarketingCampaignId(marketingCampaignEntity.getId());
                customerBaseEntityList.add(customerBaseEntity);
            }
            boolean saveOk =customerBaseEntityService.saveBatch(customerBaseEntityList);
            if (!saveOk){
                return Result.error("修改失败");
            }
        }
        return Result.ok();
    }
}
