package com.hfkj.service.impl;

import com.hfkj.dao.BsStoreDiscountActivityMapper;
import com.hfkj.entity.BsStoreDiscountActivity;
import com.hfkj.entity.BsStoreDiscountActivityExample;
import com.hfkj.entity.BsStoreDiscountActivityPartakeUser;
import com.hfkj.service.BsStoreDiscountActivityPartakeUserService;
import com.hfkj.service.BsStoreDiscountActivityService;
import com.hfkj.sysenum.StoreDiscountActivityStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsStoreDiscountActivityServiceImpl
 * @author: HuRui
 * @date: 2022/11/21
 **/
@Service("storeDiscountActivityService")
public class BsStoreDiscountActivityServiceImpl implements BsStoreDiscountActivityService {

    @Resource
    private BsStoreDiscountActivityMapper storeDiscountActivityMapper;

    @Resource
    private BsStoreDiscountActivityPartakeUserService storeDiscountActivityPartakeUserService;

    @Override
    public BsStoreDiscountActivity editActivity(BsStoreDiscountActivity discountActivity) {
        if (discountActivity.getId() == null) {
            discountActivity.setCreateTime(new Date());
            discountActivity.setUpdateTime(new Date());
            storeDiscountActivityMapper.insert(discountActivity);
        } else {
            discountActivity.setUpdateTime(new Date());
            storeDiscountActivityMapper.updateByPrimaryKey(discountActivity);
        }
        return discountActivity;
    }


    @Override
    public BsStoreDiscountActivity getDetailById(Long id) {
        return storeDiscountActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsStoreDiscountActivity> getUseDiscountListByPhone(String phone) {
        List<BsStoreDiscountActivity> activityList = new ArrayList<>();
        List<BsStoreDiscountActivityPartakeUser> list = storeDiscountActivityPartakeUserService.getUseListByPhone(phone);
        for (BsStoreDiscountActivityPartakeUser partakeUser : list) {
            activityList.add(getDetailById(partakeUser.getDiscountActivityId()));
        }
        return activityList;
    }

    @Override
    public List<BsStoreDiscountActivity> getActivityList(Map<String, Object> param) {
        BsStoreDiscountActivityExample example = new BsStoreDiscountActivityExample();
        BsStoreDiscountActivityExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(StoreDiscountActivityStatusEnum.status0.getNumber());

        if (MapUtils.getInteger(param, "partakeWay") != null) {
            criteria.andPartakeWayEqualTo(MapUtils.getInteger(param, "partakeWay"));
        }

        if (MapUtils.getLong(param, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(param, "companyId"));
        }

        if (MapUtils.getLong(param, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(param, "merId"));
        }

        if (MapUtils.getLong(param, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(param, "storeId"));
        }

        if (MapUtils.getLong(param, "storeIdNull") != null) {
            criteria.andStoreIdIsNull();
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "storeName"))) {
            criteria.andStoreNameLike("%" + MapUtils.getString(param, "storeName") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "name"))) {
            criteria.andNameLike("%"+MapUtils.getString(param, "name")+"%");
        }

        if (MapUtils.getLong(param, "endTimeS") != null) {
            criteria.andEndTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "endTimeS")));
        }

        if (MapUtils.getLong(param, "endTimeE") != null) {
            criteria.andEndTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "endTimeE")));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return storeDiscountActivityMapper.selectByExample(example);
    }
}
