package cn.ysk.cashier.service.impl.shopimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.dto.shop.ShopInfoUpdateDTO;
import cn.ysk.cashier.dto.shop.ShopMsgRemoveDTO;
import cn.ysk.cashier.dto.shop.ShopMsgStateDTO;
import cn.ysk.cashier.mybatis.entity.TbShopMsgState;
import cn.ysk.cashier.mybatis.entity.TbShopOpenId;
import cn.ysk.cashier.mybatis.mapper.TbShopOpenIdMapper;
import cn.ysk.cashier.mybatis.service.TbShopMsgStateService;
import cn.ysk.cashier.pojo.shop.TbFullShopId;
import cn.ysk.cashier.service.shop.MsgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class MsgServiceImpl implements MsgService {
    private final TbShopOpenIdMapper shopOpenIdMapper;
    private final TbShopMsgStateService shopMsgStateService;

    public MsgServiceImpl(TbShopOpenIdMapper shopOpenIdMapper, TbShopMsgStateService shopMsgStateService) {
        this.shopOpenIdMapper = shopOpenIdMapper;
        this.shopMsgStateService = shopMsgStateService;
    }

    @Override
    public Page<TbFullShopId> all(Integer page, Integer size, Integer shopId, String nickName, String openId, Integer state, Integer type) {
        return shopOpenIdMapper.selectAll(shopId, nickName, openId, new Page<>(page, size));
    }

    @Override
    public Optional<Boolean> updateShopState(ShopMsgStateDTO shopMsgStateDTO) {

//        TbShopOpenId shopOpenId = new TbShopOpenId();
//        shopOpenId.setStatus(shopMsgStateDTO.getState());
//        LambdaQueryWrapper<TbShopOpenId> queryWrapper = new LambdaQueryWrapper<TbShopOpenId>().eq(TbShopOpenId::getShopId, shopMsgStateDTO.getShopId());
//        if (shopMsgStateDTO.getType() != null) {
//            queryWrapper.eq(TbShopOpenId::getType, shopMsgStateDTO.getType());
//        }
        if (shopMsgStateDTO.getType() == null) {
            shopMsgStateDTO.setType(-1);
        }
        TbShopMsgState msgState = shopMsgStateService.lambdaQuery().eq(TbShopMsgState::getType, shopMsgStateDTO.getType())
                .eq(TbShopMsgState::getShopId, shopMsgStateDTO.getShopId()).one();
        boolean flag;
        if (msgState == null) {
            msgState = new TbShopMsgState();
            msgState.setShopId(shopMsgStateDTO.getShopId());
            msgState.setCreateTime(DateUtil.date().toInstant());
            msgState.setState(shopMsgStateDTO.getType());
            flag = shopMsgStateService.save(msgState);
        }else {
            flag = shopMsgStateService.lambdaUpdate().eq(TbShopMsgState::getShopId, shopMsgStateDTO.getShopId())
                    .eq(TbShopMsgState::getType, shopMsgStateDTO.getType())
                    .set(TbShopMsgState::getState, shopMsgStateDTO.getState())
                    .set(TbShopMsgState::getUpdateTime, DateUtil.date().toInstant()).update();
        }
        return Optional.of(flag);
    }

    @Override
    public Optional<Boolean> removeSubUser(ShopMsgRemoveDTO shopMsgRemoveDTO) {
        int delete = shopOpenIdMapper.delete(new LambdaQueryWrapper<TbShopOpenId>().eq(TbShopOpenId::getShopId, shopMsgRemoveDTO.getShopId())
                .eq(TbShopOpenId::getOpenId, shopMsgRemoveDTO.getOpenId()));
        return Optional.of(delete > 0);
    }


    @Override
    public Optional<Boolean> updateInfo(ShopInfoUpdateDTO shopInfoUpdateDTO) {
        TbShopOpenId shopOpenId = new TbShopOpenId();
        if (StrUtil.isNotBlank(shopInfoUpdateDTO.getNote())) {
            shopOpenId.setNote(shopInfoUpdateDTO.getNote());
        }
        if (shopInfoUpdateDTO.getState() != null) {
            shopOpenId.setStatus(shopInfoUpdateDTO.getState());
        }
        if (shopInfoUpdateDTO.getType() != null) {
            shopOpenId.setType(shopInfoUpdateDTO.getType());
        }
        if (StrUtil.isNotBlank(shopInfoUpdateDTO.getNickName())) {
            shopOpenId.setNickname(shopInfoUpdateDTO.getNickName());
        }
        if (StrUtil.isNotBlank(shopInfoUpdateDTO.getAvatar())) {
            shopOpenId.setAvatar(shopInfoUpdateDTO.getAvatar());
        }

        if (BeanUtil.isNotEmpty(shopOpenId)) {
            shopOpenIdMapper.update(shopOpenId, new LambdaQueryWrapper<TbShopOpenId>()
                    .eq(TbShopOpenId::getOpenId, shopInfoUpdateDTO.getOpenId())
                    .eq(TbShopOpenId::getType, shopInfoUpdateDTO.getType())
                    .eq(TbShopOpenId::getShopId, shopInfoUpdateDTO.getShopId()));
        }
        return Optional.of( true);
    }

    private static final HashMap<String, String> MSG_TYPE = new HashMap<>();
    static {
        MSG_TYPE.put("0", "stockState");
        MSG_TYPE.put("1", "conState");
        MSG_TYPE.put("2", "opeState");
        MSG_TYPE.put("-1", "allState");
    }
    @Override
    public HashMap<String, Object> getInfo(Integer shopId, Integer type) {
        List<TbShopMsgState> list = shopMsgStateService.lambdaQuery().eq(TbShopMsgState::getShopId, shopId).list();
        HashMap<String, Object> data = new HashMap<>();
        MSG_TYPE.forEach((k, v) -> {
            TbShopMsgState have = null;
            for (TbShopMsgState tbShopMsgState : list) {
                if (tbShopMsgState.getType().equals(Integer.valueOf(k))) {
                    have = tbShopMsgState;
                    break;
                }
            }
            if (have == null) {
                TbShopMsgState msgState = new TbShopMsgState();
                msgState.setShopId(shopId);
                msgState.setCreateTime(DateUtil.date().toInstant());
                msgState.setType(Integer.valueOf(k));
                msgState.setState(0);
                shopMsgStateService.save(msgState);
            }
            data.put(v, have != null ? have.getState() : 0);
        });

        return data;
    }
}
