package com.ruoyi.system.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ObjectUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.system.domain.InterestsNumberTable;
import com.ruoyi.system.domain.InterestsTable;
import com.ruoyi.system.domain.SettledMerchants;
import com.ruoyi.system.mapper.InterestsNumberTableMapper;
import com.ruoyi.system.mapper.InterestsTableMapper;
import com.ruoyi.system.mapper.SettledMerchantsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.service.IInterestsTableService;

/**
 * 商户权益记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-08
 */
@Service
public class InterestsTableServiceImpl implements IInterestsTableService 
{
    @Autowired
    private InterestsTableMapper interestsTableMapper;
    @Autowired
    private InterestsNumberTableMapper interestsNumberTableMapper;
    @Autowired
    private SettledMerchantsMapper settledMerchantsMapper;

    /**
     * 查询商户权益记录
     * 
     * @param id 商户权益记录主键
     * @return 商户权益记录
     */
    @Override
    public InterestsTable selectInterestsTableById(Long id)
    {
        return interestsTableMapper.selectInterestsTableById(id);
    }

    /**
     * 查询商户权益记录列表
     * 
     * @param interestsTable 商户权益记录
     * @return 商户权益记录
     */
    @Override
    public List<InterestsTable> selectInterestsTableList(InterestsTable interestsTable)
    {
        startPage();
        List<InterestsTable> interestsTableList = interestsTableMapper.selectInterestsTableList(interestsTable);

        //拼接权益等级和权益次数
        for (InterestsTable table : interestsTableList) {
            StringBuilder interestsLevel = new StringBuilder();
            StringBuilder interestsNumber = new StringBuilder();
            for (int i=0;i<table.getInterestsNumberTableList().size();i++) {
                if(i==0){
                    interestsLevel.append(table.getInterestsNumberTableList().get(i).getRightsRank());
                    interestsNumber.append(table.getInterestsNumberTableList().get(i).getNumber());
                }else{
                    interestsLevel.append(",").append(table.getInterestsNumberTableList().get(i).getRightsRank());
                    interestsNumber.append(",").append(table.getInterestsNumberTableList().get(i).getNumber());
                }
            }
            table.setInterestsLevel(interestsLevel.toString());
            table.setInterestsNumber(interestsNumber.toString());
        }

        return interestsTableList;
    }

    /**
     * 新增商户权益记录
     * 
     * @param interestsTable 商户权益记录
     * @return 结果
     */
    @Override
    public AjaxResult insertInterestsTable(InterestsTable interestsTable)
    {

        List<InterestsNumberTable> interestsNumberTableList = interestsTable.getInterestsNumberTableList();
        //查询商户的权益所属政策类型id
        //SettledMerchants settledMerchants = settledMerchantsMapper.selectSettledMerchantsByBusinessId(interestsTable.getMerchantsId());

        //interestsTable.setInterestsPolicyId(settledMerchants.getPolicyId());
        interestsTable.setCreateTime(DateUtils.getNowDate());
        int count = interestsTableMapper.insertInterestsTable(interestsTable);
        if (count < 1) return AjaxResult.error("新增权益信息失败");
        int childCount = 0;
        for (InterestsNumberTable interestsNumberTable : interestsNumberTableList) {
            interestsNumberTable.setInterestsId(interestsTable.getId());
            childCount += interestsNumberTableMapper.insertInterestsNumberTable(interestsNumberTable);
        }
        return childCount > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改商户权益记录
     * 
     * @param interestsTable 商户权益记录
     * @return 结果
     */
    @Override
    public AjaxResult updateInterestsTable(InterestsTable interestsTable)
    {
        List<InterestsNumberTable> interestsNumberTableList = interestsTable.getInterestsNumberTableList();

        interestsTable.setUpdateTime(DateUtils.getNowDate());
        interestsTableMapper.updateInterestsTable(interestsTable);
        for (InterestsNumberTable interestsNumberTable : interestsNumberTableList) {
            if(interestsNumberTable.getNumberId()!=null && interestsNumberTable.getNumberId()!=0){
                interestsNumberTable.setId(interestsNumberTable.getNumberId());
                interestsNumberTableMapper.updateInterestsNumberTable(interestsNumberTable);
            }else{
                interestsNumberTable.setInterestsId(interestsTable.getId());
                interestsNumberTableMapper.insertInterestsNumberTable(interestsNumberTable);
            }
        }

        return AjaxResult.success();
    }

    /**
     * 批量删除商户权益记录
     * 
     * @param ids 需要删除的商户权益记录主键
     * @return 结果
     */
    @Override
    public int deleteInterestsTableByIds(Long[] ids)
    {
        return interestsTableMapper.deleteInterestsTableByIds(ids);
    }

    /**
     * 删除商户权益记录信息
     * 
     * @param id 商户权益记录主键
     * @return 结果
     */
    @Override
    public int deleteInterestsTableById(Long id)
    {
        return interestsTableMapper.deleteInterestsTableById(id);
    }


    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
