package com.iamberry.wechat.service.integral;

import com.github.pagehelper.PageHelper;
import com.iamberry.wechat.core.entity.integral.*;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.face.integral.UseIntegralService;
import com.iamberry.wechat.service.mapper.MemberMapper;
import com.iamberry.wechat.service.mapper.UseIntegralMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.PageUtil;

import java.beans.Transient;
import java.util.Date;
import java.util.List;
@Service
public class UseIntegralServiceImpl implements UseIntegralService {

    private Logger logger = LoggerFactory.getLogger(UseIntegralServiceImpl.class);

    @Autowired
    private UseIntegralMapper useIntegralMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 获取会员信息.会员等级名称
     * @param openId
     * @return
     */
    @Override
    public MemberIntegral getMemberInfoAndRankName(String openId) {
        return useIntegralMapper.getMemberInfoAndRankName(openId);
    }

    /**
     * 获得的总积分数
     * @param userOpenId
     * @return
     */
    @Override
    public Integer getGainIntegralCount(String userOpenId) {
        return useIntegralMapper.getGainIntegralCount(userOpenId);
    }

    /**
     * 使用的总积分数
     * @param openid
     * @return
     */
    @Override
    public Integer getUseIntegralCount(String openid) {
        return useIntegralMapper.getUseIntegralCount(openid);
    }

    /**
     * 待入账的总积分数
     * @param userOpenId
     * @return
     */
    @Override
    public Integer getStayIntegralCount(String userOpenId) {
        return useIntegralMapper.getStayIntegralCount(userOpenId);
    }

    /**
     * 全部积分记录
     * @param useIntegral
     * @return
     */
    @Override
    public List<UseIntegral> listIntegralInfo(UseIntegral useIntegral) {
        return useIntegralMapper.listIntegralInfo(useIntegral);
    }

    @Override
    public List<IntegralConfig> listIntegralConfig(IntegralConfig integralConfig) {
        return useIntegralMapper.listIntegralConfig(integralConfig);
    }

    /**
     * 查询会员等级信息
     * @return
     */
    @Override
    public List<RankRule> getRankRule() {
        return useIntegralMapper.getRankRule();
    }

    /**
     * 查询会员等级信息
     * @param  id
     * @return  rankRule
     */
    @Override
    public  RankRule  getRankRuleById(Integer  id){
        return  useIntegralMapper.getRankRuleById(id);
    }

    /**
     * 查询待入账积分的集合
     * @return
     */
    @Override
    public List<StayIntegral>  listStayIntegral(StayIntegral stayIntegral){
        return useIntegralMapper.listStayIntegral(stayIntegral);
    }

    @Override
    public PagedResult<UseIntegral> listUseIntegral(PageRequest<UseIntegral> pageRequest) {
        PageHelper.startPage(pageRequest.getPageNO(), pageRequest.getPageSize(), pageRequest.isPageTotal());
        List<UseIntegral> detedctList = useIntegralMapper.listUseIntegral(pageRequest.getData());
        return PageUtil.getPage(detedctList);
    }

    @Override
    public Integer listUseIntegralCount(UseIntegral useIntegral) {
        return useIntegralMapper.listUseIntegralCount(useIntegral);
    }

    @Override
    public PagedResult<StayIntegral> listAdminStayIntegral(PageRequest<StayIntegral> pageRequest) {
        PageHelper.startPage(pageRequest.getPageNO(), pageRequest.getPageSize(), pageRequest.isPageTotal());
        List<StayIntegral> detedctList = useIntegralMapper.listAdminStayIntegral(pageRequest.getData());
        return PageUtil.getPage(detedctList);
    }

    @Override
    public Integer listAdminStayIntegralCount(StayIntegral stayIntegral) {
        return useIntegralMapper.listAdminStayIntegralCount(stayIntegral);
    }

    @Override
    public Integer updatestayIntegralStatus(StayIntegral stayIntegral) {
        return useIntegralMapper.updatestayIntegralStatus(stayIntegral);
    }

    @Override
    @Transient
    public boolean updateIntegralStayToAlready() {
        StayIntegral stayIntegral = new StayIntegral();
        stayIntegral.setStayStatus(1);
        stayIntegral.setStayRecordedTime(new Date());
        /*获取所有的待入账积分*/
        List<StayIntegral>  stayIntegralList = useIntegralMapper.listStayIntegral(stayIntegral);
        for(StayIntegral si : stayIntegralList ){
		    /*增加入账积分，累计积分，减去待入账积分*/
            Member member = memberMapper.getMemberByUserOpenId(si.getUserOpenId());
            Member mem = new Member();
            mem.setUserOpenid(si.getUserOpenId());
            mem.setUserSurplusIntegral(member.getUserSurplusIntegral() + si.getStayNumber());
            mem.setUserIntegral(member.getUserIntegral() + si.getStayNumber());
            mem.setUserStayIntegral(member.getUserStayIntegral() - si.getStayNumber());
            Integer flag = memberMapper.updateMemberByUserId(mem);
            if (flag < 1) {
                logger.info("== 修改积分失败-->记录id："+ si.getStayId() );
                throw new RuntimeException("修改积分失败");
            }

            /*添加入账日志*/
            UseIntegral useIntegral = new UseIntegral();
            useIntegral.setUserOpenId(si.getUserOpenId());
            useIntegral.setIntegralNumber(si.getStayNumber());
            useIntegral.setStayId(si.getStayId());
            useIntegral.setIntegralBeforeNumber(member.getUserSurplusIntegral()); //变动前
            useIntegral.setIntegralEndNumber(mem.getUserSurplusIntegral()); //变动后
            useIntegral.setIntegralType(1); //入账
            useIntegral.setConfigId(si.getConfigId());
            //useIntegral.setIntegralRemarks("由待入账变更为已入账");
            flag = useIntegralMapper.addUseIntegral(useIntegral);
            if (flag < 1) {
                logger.info("== 添加入账日志失败-->记录id："+ si.getStayId() );
                throw new RuntimeException("添加入账日志失败");
            }

            /*删除日志*/
            UseIntegral ui = new UseIntegral();
            ui.setUserOpenId(si.getUserOpenId());
            ui.setStayId(si.getStayId());
            ui.setIntegralType(3);
            flag = useIntegralMapper.delUseIntegral(ui);
            if (flag < 1) {
                logger.info("== 删除待入账日志失败-->记录id："+ si.getStayId() );
                throw new RuntimeException("删除待入账日志失败");
            }

            /*再将待入账信息改为已入账状态*/
            StayIntegral stayInte =  new StayIntegral();
            stayInte.setStayId(si.getStayId());
            stayInte.setStayStatus(2);
            flag = useIntegralMapper.updateStayIntegral(stayInte);
            if (flag < 1) {
                logger.info("== 修改待入账状态失败-->记录id："+ si.getStayId() );
                throw new RuntimeException("修改待入账状态失败");
            }
        }
		/*增加入账记录*/
        return true;
    }

    /**
     * 查询单条数据
     * @param  id
     * @return  integralConfig
     */
    @Override
    public  IntegralConfig  getIntegralConfigById(Integer  id){
        return  useIntegralMapper.getIntegralConfigById(id);
    }

    /**
     * 获取集合
     * @param  useIntegral
     * @return List
     */
    @Override
    public  List<UseIntegral>  getUseIntegralList(UseIntegral  useIntegral){
        return  useIntegralMapper.getUseIntegralList(useIntegral);
    }

}
