package com.yijia.integral.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.yijia.common.core.domain.entity.SysDictData;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.customer.domain.CustomerCardSetting;
import com.yijia.customer.domain.CustomerGradeSetting;
import com.yijia.customer.mapper.CustomerCardSettingMapper;
import com.yijia.customer.mapper.CustomerGradeSettingMapper;
import com.yijia.integral.domain.IntegralRuleDetail;
import com.yijia.integral.domain.IntegralShopPic;
import com.yijia.integral.mapper.IntegralRuleDetailMapper;
import com.yijia.integral.mapper.IntegralShopPicMapper;
import com.yijia.station.domain.StationOilPrice;
import com.yijia.station.mapper.StationOilPriceMapper;
import com.yijia.station.service.impl.StationOilPriceServiceImpl;
import com.yijia.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yijia.integral.mapper.IntegralRuleMapper;
import com.yijia.integral.domain.IntegralRule;
import com.yijia.integral.service.IIntegralRuleService;

/**
 * 积分规则Service业务层处理
 *
 * @author yijia
 * @date 2021-02-24
 */
@Service
public class IntegralRuleServiceImpl implements IIntegralRuleService
{
    @Autowired
    private IntegralRuleMapper integralRuleMapper;
    @Autowired
    private IntegralRuleDetailMapper integralRuleDetailMapper;
    @Autowired
    private IntegralShopPicMapper integralShopPicMapper;
    @Autowired
    private CustomerGradeSettingMapper customerGradeSettingMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    /**
     * 查询积分规则
     *
     * @param id 积分规则ID
     * @return 积分规则
     */
    @Override
    public IntegralRule selectIntegralRuleById(Long id)
    {
        //查询油品数据
        IntegralRule integralRule =integralRuleMapper.selectIntegralRuleById(id);
        IntegralRuleDetail  detail  = new  IntegralRuleDetail();
        detail.setParentId(id);
        List<IntegralRuleDetail> list =  integralRuleDetailMapper.selectIntegralRuleDetailList(detail);
        integralRule.setItemsDetail(list);
        //查询图片信息
        List<IntegralShopPic> shopPicList =new ArrayList<>();
        if(integralRule!=null && integralRule.getImage1() !=null && !integralRule.getImage1().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("1");
            pic.setUrl(integralRule.getImage1());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage2() !=null && !integralRule.getImage2().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("2");
            pic.setUrl(integralRule.getImage2());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage3() !=null && !integralRule.getImage3().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("3");
            pic.setUrl(integralRule.getImage3());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage4() !=null && !integralRule.getImage4().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("4");
            pic.setUrl(integralRule.getImage4());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage5() !=null && !integralRule.getImage5().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("5");
            pic.setUrl(integralRule.getImage5());
            shopPicList.add(pic);
        }
        integralRule.setImgFileList(shopPicList);
        return integralRule;
    }

    /**
     * 查询积分规则列表
     *
     * @param integralRule 积分规则
     * @return 积分规则
     */
    @Override
    public List<IntegralRule> selectIntegralRuleList(IntegralRule integralRule)
    {
        return integralRuleMapper.selectIntegralRuleList(integralRule);
    }

    /**
     * 新增
     *
     * @param integralRule
     * @return 结果
     */
    @Override
    public int insertIntegralRule(IntegralRule integralRule)
    {
        if(integralRule.getIntegralProportion()==null){
            integralRule.setIntegralProportion("1");
        }
        if(integralRule.getDatePickerTime()==null){
            integralRule.setDatePickerTime("");
        }
        if(integralRule.getDatePicker()==null){
            integralRule.setDatePicker("");
        }
        if(integralRule.getImgFileList()!=null &&  integralRule.getImgFileList().size()>0){
            List<IntegralShopPic> imgFileList =integralRule.getImgFileList();
            for (int j=0;j<imgFileList.size();j++){
                if(j==0){
                    integralRule.setImage1(imgFileList.get(j).getUrl());
                }else if(j==1){
                    integralRule.setImage2(imgFileList.get(j).getUrl());
                }else if(j==2){
                    integralRule.setImage3(imgFileList.get(j).getUrl());
                }else if(j==3){
                    integralRule.setImage4(imgFileList.get(j).getUrl());
                }else if(j==4){
                    integralRule.setImage5(imgFileList.get(j).getUrl());
                }
            }
        }
        int i =integralRuleMapper.insertIntegralRule(integralRule);
      //  IntegralRule rule=  integralRuleMapper.insertIntegralRule(integralRule);
        //判断规则明细
        if(integralRule.getItemsDetail()!=null &&  integralRule.getItemsDetail().size()>0){
            List<IntegralRuleDetail> list =integralRule.getItemsDetail();
            for (IntegralRuleDetail detail : list){
                if(detail.getOilName()!=null && detail.getOilName()!=""){
                    //查询当前油品对应的
                    String dataValue= dictDataMapper.selectDictValue("oil_name",detail.getOilName());
                    detail.setOilType(dataValue);
                }else{
                    i=0;
                    return i;
                }
                if(detail.getSaleAmt()!=null  && detail.getIntegral()!=null && detail.getSaleAmt().compareTo(new BigDecimal("0"))==1){
                    detail.setParentId(integralRule.getId().longValue());
                    i = integralRuleDetailMapper.insertIntegralRuleDetail(detail);
                    if(i==0){
                        return i;
                    }
                }
            }
        }

        return i;
    }

    /**
     * 修改积分规则
     *
     * @param integralRule 积分规则
     * @return 结果
     */
    @Override
    public int updateIntegralRule(IntegralRule integralRule)
    {
        //删除对应的明细表数据
        IntegralRuleDetail ruleDetail = new IntegralRuleDetail();
        ruleDetail.setParentId(integralRule.getId());
        integralRuleDetailMapper.deleteIntegralRuleDetail(ruleDetail);
        //从新保存数据
        if(integralRule.getItemsDetail()!=null &&  integralRule.getItemsDetail().size()>0){
            List<IntegralRuleDetail> list =integralRule.getItemsDetail();
            for (IntegralRuleDetail detail : list){
                if(detail.getOilName()!=null && detail.getOilName()!=""){
                    //查询当前油品对应的
                    String dataValue= dictDataMapper.selectDictValue("oil_name",detail.getOilName());
                    detail.setOilType(dataValue);
                }
                if(detail.getSaleAmt()!=null  && detail.getIntegral()!=null && detail.getSaleAmt().compareTo(new BigDecimal("0"))==1){
                    detail.setParentId(integralRule.getId().longValue());
                    integralRuleDetailMapper.insertIntegralRuleDetail(detail);
                }
            }
        }
        if(integralRule.getImgFileList()!=null &&  integralRule.getImgFileList().size()>0){
            List<IntegralShopPic> imgFileList =integralRule.getImgFileList();
            for (int j=0;j<imgFileList.size();j++){
                if(j==0){
                    integralRule.setImage1(imgFileList.get(j).getUrl());
                }else if(j==1){
                    integralRule.setImage2(imgFileList.get(j).getUrl());
                }else if(j==2){
                    integralRule.setImage3(imgFileList.get(j).getUrl());
                }else if(j==3){
                    integralRule.setImage4(imgFileList.get(j).getUrl());
                }else if(j==4){
                    integralRule.setImage5(imgFileList.get(j).getUrl());
                }
            }
        }
        return  integralRuleMapper.updateIntegralRule(integralRule);
    }

    /**
     * 批量删除积分规则
     * insertIntegralRule
     * @param ids 需要删除的积分规则ID
     * @return 结果
     */
    @Override
    public int deleteIntegralRuleByIds(Long[] ids)
    {
        return integralRuleMapper.deleteIntegralRuleByIds(ids);
    }

    /**
     * 删除积分规则信息
     *
     * @param id 积分规则ID
     * @return 结果
     */
    @Override
    public int deleteIntegralRuleById(Long id)
    {
        return integralRuleMapper.deleteIntegralRuleById(id);
    }

    /****
     * 获取当前站的积分规则
     * @param integralRule
     * @return
     */
    @Override
    public IntegralRule selectIntegralRule(IntegralRule integralRule) {
        IntegralRule rule = integralRuleMapper.selectIntegralRule(integralRule);
        if(rule!=null){
            LoginUser currentUser = SecurityUtils.getLoginUser();
            IntegralRuleDetail  detail  = new  IntegralRuleDetail();
            detail.setParentId(rule.getId());
            List<IntegralRuleDetail> list =  integralRuleDetailMapper.selectIntegralRuleDetailList(detail);
            if(list!=null && list.size()>0){
                for(IntegralRuleDetail ruleDetail:list){
                    //查询油站的油品信息
                    if(ruleDetail.getOilName()!=null && !ruleDetail.getOilName().equals("")){
                        //查询当前油品对应的所有的等级
                        CustomerGradeSetting  customerGradeSetting =new CustomerGradeSetting();
                        customerGradeSetting.setDelFlag("0");
                        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
                        customerGradeSetting.setOilName(ruleDetail.getOilName());
                        List<CustomerGradeSetting> greadList = customerGradeSettingMapper.selectCustomerGradeSettingList(customerGradeSetting);
                        ruleDetail.setGradeList(greadList);
                    }
                }
            }else{
                IntegralRuleDetail integralRuleDetail = new IntegralRuleDetail();
                list.add(integralRuleDetail);
            }
            rule.setItemsDetail(list);
            //查询图片信息
            List<IntegralShopPic> shopPicList =new ArrayList<>();
            if(integralRule!=null && integralRule.getImage1() !=null && !integralRule.getImage1().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("1");
                pic.setUrl(integralRule.getImage1());
                shopPicList.add(pic);
            }
            if(integralRule!=null && integralRule.getImage2() !=null && !integralRule.getImage2().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("2");
                pic.setUrl(integralRule.getImage2());
                shopPicList.add(pic);
            }
            if(integralRule!=null && integralRule.getImage3() !=null && !integralRule.getImage3().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("3");
                pic.setUrl(integralRule.getImage3());
                shopPicList.add(pic);
            }
            if(integralRule!=null && integralRule.getImage4() !=null && !integralRule.getImage4().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("4");
                pic.setUrl(integralRule.getImage4());
                shopPicList.add(pic);
            }
            if(integralRule!=null && integralRule.getImage5() !=null && !integralRule.getImage5().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("5");
                pic.setUrl(integralRule.getImage5());
                shopPicList.add(pic);
            }
            integralRule.setImgFileList(shopPicList);
        }else{
            rule =new IntegralRule();
            rule.setIntegralFlag("0");
            rule.setIntegralActivity("0");
            rule.setIntegralProportion("1");
            rule.setIntegralTermSetting("1");
            rule.setTermDateManage("1");
            List<IntegralRuleDetail> list = new ArrayList<IntegralRuleDetail>();
            IntegralRuleDetail integralRuleDetail = new IntegralRuleDetail();
            list.add(integralRuleDetail);
            List<IntegralShopPic> shopPicList =new ArrayList<>();
            integralRule.setImgFileList(shopPicList);
            rule.setItemsDetail(list);
        }
        return rule;
    }

    /**
     * 查询积分明细
     * @param integralRule
     * @return
     */
    @Override
    public List<IntegralRule> listRuleInfo(IntegralRule integralRule) {
        return integralRuleMapper.listRuleInfo(integralRule);
    }
}
