package com.xhpc.activity.service.impl;

import com.xhpc.activity.domain.XhpcActivityDiscountDomain;
import com.xhpc.activity.domain.XhpcActivityTemplateDomain;
import com.xhpc.activity.mapper.XhpcActivityDiscountMapper;
import com.xhpc.activity.mapper.XhpcActivityTemplateMapper;
import com.xhpc.activity.service.XhpcActivityDiscountService;
import com.xhpc.activity.utils.AreaCodeUtil;
import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


@Service
@Transactional
public class XhpcActivityDiscountServiceImpl extends BaseService implements XhpcActivityDiscountService  {


    @Resource
    XhpcActivityDiscountMapper discountMapper;

    @Resource
    XhpcActivityTemplateMapper templateMapper;


    @Override
    public List<XhpcActivityDiscountDomain> getPage(Map<String, Object> params){
        return discountMapper.selectByParams(params);
    }


    @Override
    public List<Map<String, Object>> getNameList(Map<String, Object> params){
        return discountMapper.selectNameMapList(params);
    }


    @Override
    public XhpcActivityDiscountDomain getDomainByPk(Integer activityId) {
        XhpcActivityDiscountDomain discountDomain = discountMapper.selectByPrimaryKey(activityId);
        discountDomain.setTemplateDomain(templateMapper.selectByPrimaryKey(discountDomain.getTemplateId()));
        return discountDomain;
    }


    @Override
    public boolean insertDomain(XhpcActivityDiscountDomain domain){

        XhpcActivityTemplateDomain templateDomain = templateMapper.selectByPrimaryKey(domain.getTemplateId());
        if(templateDomain == null){
            throw new CustomException("折扣模版不存在");
        }
        domain.setStationList(AreaCodeUtil.removeAreaCode(domain.getStationList()));
        discountMapper.insert(domain);
        return true;
    }




    @Override
    public boolean updateDomain(XhpcActivityDiscountDomain domain){
        domain.setStationList(AreaCodeUtil.removeAreaCode(domain.getStationList()));
        return discountMapper.updateByPrimaryKey(domain) > 0;
    }


    @Override
    public boolean checkDomain(XhpcActivityDiscountDomain domain){
        return discountMapper.checkByPrimaryKey(domain) > 0;
    }


    @Override
    public boolean updateDomainStatus(XhpcActivityDiscountDomain domain){
        return discountMapper.updateStatusByPrimaryKey(domain) > 0;
    }


    @Override
    public boolean deleteByPk(Integer activityId){
        return discountMapper.deleteLogicByPrimaryKey(activityId) > 0;
    }


    @Override
    public List<Map<String, Object>> getUserList(Map<String, Object> params){
        XhpcActivityDiscountDomain discountDomain = discountMapper.selectByPrimaryKey(Integer.parseInt(params.get("activityId").toString()));


        String appUserIds = "";
        if(StringUtils.isNotEmpty(discountDomain.getAppUserList())){
            appUserIds = discountDomain.getAppUserList();
        }

        String comUserIds = "";
        if(StringUtils.isNotEmpty(discountDomain.getComUserList())){
            comUserIds = discountDomain.getComUserList();
        }

        String bUserIds = "";
        if(StringUtils.isNotEmpty(discountDomain.getCusUserList())){
            bUserIds = discountDomain.getCusUserList();
        }

        params.put("appUserIds", appUserIds);
        params.put("comUserIds", comUserIds);
        params.put("bUserIds", bUserIds);

        startPage();
        List<Map<String, Object>> userList = discountMapper.selectUserListByParams(params);
        return userList;
    }
}
