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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hoteladdress.entity.HotelAddress;
import org.jeecg.modules.hoteladdress.service.IHotelAddressService;
import org.jeecg.modules.xcx.address.req.AddressPageReq;
import org.jeecg.modules.xcx.address.service.IAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class AddressServiceImpl implements IAddressService {

    @Resource
    private IHotelAddressService hotelAddressService;
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public Result<?> addressPage(AddressPageReq addressPageReq) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(addressPageReq.getToken());
        Page<HotelAddress> page = new Page<>(addressPageReq.getPageNo(), addressPageReq.getPageSize());
        Page<HotelAddress> addressPage = hotelAddressService.lambdaQuery()
                .eq(HotelAddress::getDelFlag, 0)
                .eq(HotelAddress::getUserId, hanHaiMember.getId())
                .orderByDesc(HotelAddress::getDefaultFlag)
                .orderByDesc(HotelAddress::getCreateTime)
                .page(page);
        return Result.ok(addressPage);
    }

    @Override
    public Result<?> addressAdd(HotelAddress hotelAddress) {
        Integer i = (Integer) redisUtil.get("addressAdd:" + hotelAddress.getUserId());
        if(i != null){
            throw new JeecgBootException("请勿重复提交");
        }
        if(StringUtils.isBlank(hotelAddress.getName())){
            throw new JeecgBootException("联系人不能为空");
        }
        if(StringUtils.isBlank(hotelAddress.getPhone())){
            throw new JeecgBootException("请填写手机号");
        }
        if(!StringUtils.isNumeric(hotelAddress.getPhone()) || hotelAddress.getPhone().length() != 11){
            throw new JeecgBootException("请填写正确的手机号");
        }
        if(StringUtils.isBlank(hotelAddress.getAddress())){
            throw new JeecgBootException("请选择所在地区");
        }
        if(StringUtils.isBlank(hotelAddress.getAddressDetail())){
            throw new JeecgBootException("请填写详细地址");
        }
        boolean save = hotelAddressService.save(hotelAddress);
        redisUtil.set("addressAdd:" + hotelAddress.getUserId(),1,10);
        return Result.OK();
    }

    @Override
    public Result<?> addressEdit(HotelAddress hotelAddress) {
        Integer i = (Integer) redisUtil.get("addressEdit:" + hotelAddress.getUserId());
        if(i != null){
            throw new JeecgBootException("请勿重复提交");
        }
        if(StringUtils.isBlank(hotelAddress.getName())){
            throw new JeecgBootException("联系人不能为空");
        }
        if(StringUtils.isBlank(hotelAddress.getPhone())){
            throw new JeecgBootException("请填写手机号");
        }
        if(!StringUtils.isNumeric(hotelAddress.getPhone()) || hotelAddress.getPhone().length() != 11){
            throw new JeecgBootException("请填写正确的手机号");
        }
        if(StringUtils.isBlank(hotelAddress.getAddress())){
            throw new JeecgBootException("请选择所在地区");
        }
        if(StringUtils.isBlank(hotelAddress.getAddressDetail())){
            throw new JeecgBootException("请填写详细地址");
        }
        boolean b = hotelAddressService.updateById(hotelAddress);
        redisUtil.set("addressEdit:" + hotelAddress.getUserId(),1,10);
        return Result.OK();
    }

    @Transactional
    @Override
    public Result<?> defaultAddress(String id,String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        HotelAddress hotelAddress = new HotelAddress();
        HotelAddress hotelAddress1 = hotelAddressService.getById(id);
        if(hotelAddress1 == null || !hotelAddress1.getUserId().equals(hanHaiMember.getId())){
            throw new JeecgBootException("数据错误，请刷新重试");
        }
        if(hotelAddress1.getDefaultFlag() == 1) {
            hotelAddress.setId(hotelAddress1.getId());
            hotelAddress.setDefaultFlag(0);
            hotelAddressService.updateById(hotelAddress);
            return Result.OK();
        }
            HotelAddress hotelAddress2 = hotelAddressService.lambdaQuery()
                    .eq(HotelAddress::getDefaultFlag, 1)
                    .eq(HotelAddress::getDelFlag, 0)
                    .eq(HotelAddress::getUserId, hanHaiMember.getId())
                    .one();
        if(hotelAddress2 != null){
            HotelAddress hotelAddress3 = new HotelAddress();
            hotelAddress3.setId(hotelAddress2.getId());
            hotelAddress3.setDefaultFlag(0);
            hotelAddressService.updateById(hotelAddress3);
        }



        hotelAddress.setId(hotelAddress1.getId());
        hotelAddress.setDefaultFlag(1);
        hotelAddressService.updateById(hotelAddress);
        return Result.OK();
    }

    @Override
    public Result<?> deleteAddress(String id, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        HotelAddress hotelAddress = new HotelAddress();
        HotelAddress hotelAddress1 = hotelAddressService.getById(id);
        if(hotelAddress1 == null || !hotelAddress1.getUserId().equals(hanHaiMember.getId())){
            throw new JeecgBootException("数据错误，请刷新重试");
        }
        hotelAddressService.removeById(hotelAddress1);
        return Result.OK();
    }
}
