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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.activity.CustomSecSpecBean;
import com.gree.mall.manager.bean.wxCustomer.SimpleTagBean;
import com.gree.mall.manager.bean.wxCustomer.WxCustomerBean;
import com.gree.mall.manager.bean.wxCustomer.WxCustomerTagBean;
import com.gree.mall.manager.commonmapper.WxCustomerMapper;
import com.gree.mall.manager.plus.entity.UserTag;
import com.gree.mall.manager.plus.service.UserTagService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Service
public class WxCustomerLogic {

    @Autowired
    private UserTagService userTagService;
    @Autowired
    private WxCustomerMapper wxCustomerMapper;

    public IPage<WxCustomerBean> list( String keyword,String[] tagIds, Date startTime,Date endtTime,Integer pageNum,Integer pageSize){
        Integer count = wxCustomerMapper.queryCustomerCount(keyword,startTime,endtTime,tagIds);
        List<WxCustomerBean> wxCustomerBeans = null;
        if (count > 0) {
            wxCustomerBeans = wxCustomerMapper.queryCustomerList(keyword,startTime,endtTime,tagIds, (pageNum - 1) * pageSize, pageSize);
        }

        Integer totalPage = (count + pageSize - 1) / pageSize;
        Page page = new Page<WxCustomerBean>();
        page.setTotal(count);
        page.setCurrent(pageNum);
        page.setPages(totalPage);
        page.setSize(wxCustomerBeans == null ? 0 : wxCustomerBeans.size());
        page.setRecords(wxCustomerBeans);

        return page;

    }

    public WxCustomerBean detail(String externalUserId){
        return wxCustomerMapper.queryCustomerDetail(externalUserId);
    }

    @Transactional
    public void edit(WxCustomerTagBean wxCustomerTagBean){
        if(wxCustomerTagBean!=null){
            if(wxCustomerTagBean.getDelTags()!=null&&wxCustomerTagBean.getDelTags().size()>0){
                for(SimpleTagBean simpleTagBean:wxCustomerTagBean.getDelTags()){
                    userTagService.lambdaUpdate()
                            .eq(UserTag::getUnionId,wxCustomerTagBean.getUnionId())
                            .eq(UserTag::getTagId,simpleTagBean.getTagId())
                            .remove();
                }
            }
            UserTag userTag = new UserTag();
            if(wxCustomerTagBean.getAddTags()!=null&&wxCustomerTagBean.getAddTags().size()>0){
                for(SimpleTagBean simpleTagBean:wxCustomerTagBean.getAddTags()){
                    userTag.setUserTagId(null);
                    userTag.setUnionId(wxCustomerTagBean.getUnionId());
                    userTag.setTagId(simpleTagBean.getTagId());
                    userTag.setTagName(simpleTagBean.getTagName());
                    userTag.setAddType(false);
                    userTag.setCreateTime(new Date());
                    userTagService.save(userTag);
                }
            }

        }

    }

}
