package com.yijia.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.DateUtils;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.customer.domain.CustomerCardSettingDetail;
import com.yijia.customer.mapper.CustomerCardSettingDetailMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yijia.customer.mapper.CustomerCardSettingMapper;
import com.yijia.customer.domain.CustomerCardSetting;
import com.yijia.customer.service.ICustomerCardSettingService;

/**
 * 客户电子会员卡充值优惠设置Service业务层处理
 *
 * @author yijia
 * @date 2021-04-06
 */
@Service
public class CustomerCardSettingServiceImpl implements ICustomerCardSettingService
{
    @Autowired
    private CustomerCardSettingMapper customerCardSettingMapper;
    @Autowired
    private CustomerCardSettingDetailMapper customerCardSettingDetailMapper;

    /**
     * 查询客户电子会员卡充值优惠设置
     *
     * @param id 客户电子会员卡充值优惠设置ID
     * @return 客户电子会员卡充值优惠设置
     */
    @Override
    public CustomerCardSetting selectCustomerCardSettingById(Long id)
    {
        return customerCardSettingMapper.selectCustomerCardSettingById(id);
    }

    /**
     * 查询客户电子会员卡充值优惠设置列表
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 客户电子会员卡充值优惠设置
     */
    @Override
    public List<CustomerCardSetting> selectCustomerCardSettingList(CustomerCardSetting customerCardSetting)
    {

        return customerCardSettingMapper.selectCustomerCardSettingList(customerCardSetting);
    }

    /**
     * 查询油站电子卡规则设置
     * @param customerCardSetting
     * @return
     */
    @Override
    public CustomerCardSetting selectCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        CustomerCardSetting setting=customerCardSettingMapper.selectCustomerCardSetting(customerCardSetting);
        if(setting!=null){
            CustomerCardSettingDetail detail = new CustomerCardSettingDetail();
            detail.setParentId(setting.getId());
            detail.setDelFlag("0");
            List<CustomerCardSettingDetail> detailList =customerCardSettingDetailMapper.selectCustomerCardSettingDetailList(detail);
            if(detailList==null ){
                CustomerCardSettingDetail cardSettingDetail = new CustomerCardSettingDetail();
                detailList.add(cardSettingDetail);
            }
            setting.setDetailList(detailList);
        }else{
            setting =new  CustomerCardSetting();
            setting.setIsGradeSetting("1");
            setting.setCardEnabledFlag("0");
            setting.setIsDiscountCoupon("1");
            setting.setIsMarket("1");
            setting.setEnjoyIntegralMultiple("1");
            setting.setPresentScale("0");
            setting.setDiscountTimeSetting("0");
            setting.setDiscountTime("");
            List<CustomerCardSettingDetail> detailList =new ArrayList<>();
            setting.setDetailList(detailList);
        }
        return setting;
    }

    /**
     * 新增客户电子会员卡充值优惠设置
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 结果
     */
    @Override
    public int insertCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setCreateTime(DateUtils.getNowDate());
        customerCardSetting.setCreateBy(currentUser.getUser().getUserId()+"");
        int i =customerCardSettingMapper.insertCustomerCardSetting(customerCardSetting);
        if(customerCardSetting.getDetailList()!=null && customerCardSetting.getDetailList().size()>0){
            List<CustomerCardSettingDetail> list = customerCardSetting.getDetailList();
            for(CustomerCardSettingDetail detil:list){
                detil.setDelFlag("0");
                detil.setParentId(customerCardSetting.getId());
                detil.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                detil.setCreateTime(DateUtils.getNowDate());
                customerCardSettingDetailMapper.insertCustomerCardSettingDetail(detil);
            }
        }
        return i;
    }

    /**
     * 修改客户电子会员卡充值优惠设置
     *
     * @param customerCardSetting 客户电子会员卡充值优惠设置
     * @return 结果
     */
    @Override
    public int updateCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        int i  =customerCardSettingMapper.updateCustomerCardSetting(customerCardSetting);
        CustomerCardSettingDetail detil = new CustomerCardSettingDetail();
        detil.setParentId(customerCardSetting.getId());
        detil.setDelFlag("1");
        customerCardSettingDetailMapper.updateDetail(detil);
        if(customerCardSetting.getDetailList()!=null && customerCardSetting.getDetailList().size()>0){
            List<CustomerCardSettingDetail> list = customerCardSetting.getDetailList();
            for(CustomerCardSettingDetail settingDetil:list){
                settingDetil.setDelFlag("0");
                settingDetil.setId(null);
                settingDetil.setParentId(customerCardSetting.getId());
                settingDetil.setCreateTime(DateUtils.getNowDate());
                settingDetil.setCreateBy(currentUser.getUser().getUserId()+"");
                customerCardSettingDetailMapper.insertCustomerCardSettingDetail(settingDetil);
            }
        }
        return i;
    }

    /**
     * 批量删除客户电子会员卡充值优惠设置
     *
     * @param ids 需要删除的客户电子会员卡充值优惠设置ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardSettingByIds(Long[] ids)
    {
        return customerCardSettingMapper.deleteCustomerCardSettingByIds(ids);
    }

    /**
     * 删除客户电子会员卡充值优惠设置信息
     *
     * @param id 客户电子会员卡充值优惠设置ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardSettingById(Long id)
    {
        return customerCardSettingMapper.deleteCustomerCardSettingById(id);
    }

}
