package org.jeecg.modules.xcx.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.hotelconf.service.IHotelConfService;
import org.jeecg.modules.hotelleaselog.entity.HotelLeaseLog;
import org.jeecg.modules.hotelleaselog.service.IHotelLeaseLogService;
import org.jeecg.modules.hotelmemberaccount.entity.HotelMemberAccount;
import org.jeecg.modules.hotelmemberaccount.service.IHotelMemberAccountService;
import org.jeecg.modules.hotelshop.entity.HotelShop;
import org.jeecg.modules.hotelshop.service.IHotelShopService;
import org.jeecg.modules.xcx.user.req.EditUserReq;
import org.jeecg.modules.xcx.user.req.MyLeasePageReq;
import org.jeecg.modules.xcx.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    @Resource
    private IHotelMemberAccountService hotelMemberAccountService;
    @Resource
    private IHotelLeaseLogService hotelLeaseLogService;
    @Resource
    private IHotelShopService hotelShopService;
    @Resource
    private IHotelConfService hotelConfService;

    @Override
    public Result<?> getUserInfo(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        HotelMemberAccount hotelMemberAccount = hotelMemberAccountService.lambdaQuery()
                .eq(HotelMemberAccount::getDelFlag, 0)
                .eq(HotelMemberAccount::getUserId, hanHaiMember.getId())
                .one();
        HotelShop hotelShop = hotelShopService.lambdaQuery()
                .eq(HotelShop::getUserId, hanHaiMember.getId())
                .eq(HotelShop::getDelFlag, 0)
                .eq(HotelShop::getStatus, 1)
                .one();
        if(hotelShop != null){
            hanHaiMember.setShop(hotelShop);
        }
        hanHaiMember.setWallet(hotelMemberAccount);
        return Result.OK(hanHaiMember);
    }

    @Override
    public Result<?> editUserInfo(EditUserReq editUserReq, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);

        HanHaiMember hanHaiMember1 = new HanHaiMember();
        hanHaiMember1.setId(hanHaiMember.getId());
        if(StringUtils.isNotBlank(editUserReq.getHeadImg())){
            hanHaiMember1.setHeadImage(editUserReq.getHeadImg());
        }
        if(StringUtils.isNotBlank(editUserReq.getNickname())){
            hanHaiMember1.setNickName(editUserReq.getNickname());
        }
        hanHaiMemberService.updateById(hanHaiMember1);
        return Result.OK();
    }

    @Override
    public Result<?> myLeasePage(MyLeasePageReq myLeasePageReq) {

        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(myLeasePageReq.getToken());


        Page<HotelLeaseLog> page = new Page<>(myLeasePageReq.getPageNo(), myLeasePageReq.getPageSize());
        IPage<HotelLeaseLog> hotelLeaseLogPage = hotelLeaseLogService.myLeaseLogPage(page, hanHaiMember.getId(), myLeasePageReq.getLeaseFlag(),myLeasePageReq.getStatus());
//        Page<HotelLeaseLog> hotelLeaseLogPage = hotelLeaseLogService.lambdaQuery()
//                .eq(HotelLeaseLog::getDelFlag, 0)
//                .eq(HotelLeaseLog::getUserId, hanHaiMember.getId())
//                .orderByDesc(HotelLeaseLog::getCreateTime)
//                .page(page);

        return Result.OK(hotelLeaseLogPage);
    }

    @Override
    public Result<?> getCustom() {
        Map<String, Object> map = new HashMap<>();
        String customerService = hotelConfService.getConf("customerService");
        map.put("phone",customerService);
        return Result.OK(map);
    }

    @Override
    public Result<?> myLeaseAllDepositPrice(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        BigDecimal allDepositPrice = hotelLeaseLogService.getAllDepositPrice(hanHaiMember.getId());
        return Result.OK(allDepositPrice);
    }
}
