package cn.pluss.platform.merchantGuide.impl;

import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantGuide;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MerchantGuideMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantGuide.MerchantGuideService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-10-23
 */
@Service
public class MerchantGuideServiceImpl extends ServiceImpl<MerchantGuideMapper, MerchantGuide> implements MerchantGuideService {

    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantBaseInfoService merchantBaseInfoService;

    @Override
    public List<MerchantGuide> appList() {
        UserApp userApp = userAppService.queryUserAppByToken();
        return baseMapper.userMerchantGuide(userApp.getMerchantCode(), userApp.getMerchantName());
    }

    @Override
    public void appSave(MerchantGuide entity) {
        UserApp userApp = userAppService.queryUserAppByToken();
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getMerchantBaseInfoByUserId(userApp.getUserId() + "");

        entity.setMerchantCode(merchantBaseInfo.getMerchantCode());
        entity.setMerchantName(merchantBaseInfo.getMerchantName());

        if (entity.getId() != null) {
            MerchantGuide existData = baseMapper.selectById(entity.getId());
            entity.setStatus(existData.getStatus());
            entity.setGuideId(existData.getGuideId());
        } else {
            entity.setStatus("0");
        }


        saveOrUpdate(entity);
    }

    @Override
    public void appDel(Integer id) {
        MerchantGuide existData = baseMapper.selectById(id);

        UserApp userApp = userAppService.queryUserAppByToken();

        if (existData == null) {
            return;
        }

        if (!existData.getMerchantCode().equals(userApp.getMerchantCode())) {
            throw new MsgException("拒绝访问");
        }

        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void appChoose(MerchantGuide entity) {
        UserApp userApp = userAppService.queryUserAppByToken();
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getMerchantBaseInfoByUserId(userApp.getUserId() + "");

        if (entity.getId() != null) {
            MerchantGuide existData = baseMapper.selectById(entity.getId());

            if (existData == null) {
                return;
            }

            if (!existData.getMerchantCode().equals(userApp.getMerchantCode())) {
                throw new MsgException("拒绝访问");
            }

            if ("1".equals(existData.getStatus())) {
                return;
            }

            UpdateWrapper<MerchantGuide> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("status", "1");
            updateWrapper.set("status", "0");
            updateWrapper.eq("merchantCode", merchantBaseInfo.getMerchantCode());
            update(updateWrapper);

            existData.setStatus("1");
            updateById(existData);

        } else {
            if (entity.getGuideId() == null ) {
                throw new MsgException("缺少必要参数");
            }

            UpdateWrapper<MerchantGuide> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("status", "1");
            updateWrapper.eq("merchantCode", merchantBaseInfo.getMerchantCode());
            updateWrapper.set("status", "0");
            update(updateWrapper);

            MerchantGuide existData = new MerchantGuide()
                    .setGuideId(entity.getGuideId())
                    .setMerchantCode(merchantBaseInfo.getMerchantCode());
            existData = getOne(new QueryWrapper<>(existData));

            if (existData != null) {
                existData.setStatus("1");
                updateById(existData);
            } else {
                existData = new MerchantGuide();
                existData.setStatus("1");
                existData.setMerchantName(merchantBaseInfo.getAlias());
                existData.setMerchantCode(merchantBaseInfo.getMerchantCode());
                existData.setGuideId(entity.getGuideId());
                save(existData);
            }
        }
    }


}
