package com.kiisoo.ic.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiisoo.ic.constants.Constants;
import com.kiisoo.ic.employee.entity.QrCodeVO;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.PoiStoreStaffVO;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.service.PoiStoreStaffService;
import com.kiisoo.ic.system.entity.PrivilageUserDO;
import com.kiisoo.ic.system.enums.UserEnum;
import com.kiisoo.ic.system.mapper.PrivilageUserDOMapper;
import com.kiisoo.ic.system.service.PrivilageUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户表  服务实现类
 *
 * @author jinchaofan
 * @since 2020-02-19
 */
@Service
public class PrivilageUserServiceImpl extends ServiceImpl<PrivilageUserDOMapper, PrivilageUserDO> implements PrivilageUserService {

    /**
     * 用户 mapper
     */
    @Autowired
    private PrivilageUserDOMapper privilageUserDOMapper;

    @Autowired
    private PoiStoreStaffService parseQrCode;

    /**
     * 用户店铺 mapper
     */
    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    @Override
    public Long insertUser(String userName) {
        //新增用户
        PrivilageUserDO privilageUserDO = new PrivilageUserDO();
        privilageUserDO.setName(userName);
        privilageUserDO.setStatus(UserEnum.USER_STATUS_NORMAL.getType());
        privilageUserDO.setCreateTime(new Date());
        privilageUserDOMapper.insert(privilageUserDO);
        return privilageUserDO.getId();
    }

    @Override
    public void removeUser(Long userId) {
        privilageUserDOMapper.deleteById(userId);
    }

    @Override
    public void saveUserShopRelation(Long userId, String staffCode, Long shopId, Long type)  {
        PoiStore poiStore = poiStoreDOMapper.selectById(shopId);
        PoiStoreStaff poiStoreStaff = new PoiStoreStaff();
        poiStoreStaff.setUserId(userId);
        poiStoreStaff.setUpdateBy(Constants.SYS_OPERATION);
        poiStoreStaff.setStoreId(shopId);
        poiStoreStaff.setStaffCode("");
        poiStoreStaff.setStoreCode(poiStore.getCode());
        poiStoreStaff.setType(type);
        poiStoreStaff.setCreateTime(new Date());
        poiStoreStaff.setUpdateTime(new Date());
        poiStoreStaff.setStatus(1L);
        poiStoreStaff.setUpdateBy(Constants.SYS_OPERATION);
        poiStoreStaff.setCreateBy(Constants.SYS_OPERATION);
        poiStoreStaff.setDeleted(0);
        PoiStoreStaffVO poiStoreStaffVO = new PoiStoreStaffVO();
        poiStoreStaffVO.setStoreId(shopId);
        poiStoreStaffVO.setStaffCode(staffCode);
        //去生成一个推广人员二维码
        QrCodeVO qrCodeVO = null;
        try {
            qrCodeVO = parseQrCode.parseQrCode(poiStoreStaffVO);
            poiStoreStaff.setEpWechatQrCode(qrCodeVO.getQr_code());
            poiStoreStaff.setEpWechatConfigId(qrCodeVO.getConfig_id());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //插入
        poiStoreStaffDOMapper.insert(poiStoreStaff);

    }

    @Override
    public void removeUserShopRelation(Long userId) {
        //并不是删除了店铺只是删除了店铺中的user_id
        QueryWrapper<PoiStoreStaff> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (null != userId) {
            PoiStoreStaff poiStoreStaff = new PoiStoreStaff();
            poiStoreStaff.setUserId(0L);
            poiStoreStaffDOMapper.update(poiStoreStaff, wrapper);
        }
    }

    @Override
    public PrivilageUserDO selectById(Long userId) {
        return privilageUserDOMapper.selectById(userId);
    }

    @Override
    public Long addUser(PrivilageUserDO privilageUserDO) {
        //新增用户
        privilageUserDO.setStatus(UserEnum.USER_STATUS_NORMAL.getType());
        privilageUserDO.setCreateTime(new Date());
        privilageUserDOMapper.insert(privilageUserDO);
        return privilageUserDO.getId();
    }

}
