package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.ShShopMapper;
import com.bofeng.entity.ShShop;
import lombok.var;
import org.apache.commons.collections.CollectionUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ShShopService {

    @Autowired
    private ShShopMapper shShopMapper;

    //我的商店新增
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int addShShop(Long userId, Integer shopType, String shopName, String adddr,
                         DateTime startDate, DateTime endDate, String linkman, String phone) throws Exception {

        DateTime dateTime = new DateTime();
        var shShopDb = shShopMapper.selectById(userId);
        if (null == shShopDb) {
            var shop = new ShShop();
            shop.setShopId(IdWorker.getId());
            shop.setUserId(userId);
            shop.setShopType(shopType);
            shop.setShopName(shopName);
            shop.setAddr(adddr);
            shop.setStartDate(startDate);
            shop.setEndDate(endDate);
            shop.setLinkman(linkman);
            shop.setPhone(phone);
    /*long userId = Long.parseLong(JwtHelper.getUserId());
    qzTak.setUserId(userId);
    qzTak.setUserCreate(userId);*/
            shop.setTimeCreate(dateTime);
            /* qzTak.setUserUpdate(userId);*/
            shop.setTimeUpdate(dateTime);
            return shShopMapper.insert(shop);
        } else {
            throw new Exception("您已有一个商店，请勿重复添加");
        }
        /* else {
            shShopDb.setUserId(userId);
            shShopDb.setShopType(shopType);
            shShopDb.setShopName(shopName);
            shShopDb.setAddr(adddr);
            shShopDb.setStartDate(startDate);
            shShopDb.setEndDate(endDate);
            shShopDb.setLinkman(linkman);
            shShopDb.setPhone(phone);
    *//*long userId = Long.parseLong(JwtHelper.getUserId());
    qzTak.setUserId(userId);
    qzTak.setUserCreate(userId);*//*
            shShopDb.setTimeCreate(dateTime);
            *//* qzTak.setUserUpdate(userId);*//*
            shShopDb.setTimeUpdate(dateTime);
            return shShopMapper.updateById(shShopDb);
        }*/


    }

    public List<ShShop> queryMyShShop(Long userId) throws Exception {
        List<ShShop> shShopDb = shShopMapper.queryMyShShop(userId);
        if (CollectionUtils.isEmpty(shShopDb)) {
            throw new Exception("获取失败");
        }
        return shShopDb;
    }

//
//    public List<QzTask> selectAll() {
//        return qzTaskDao.selectAll();
//    }
//
//    public List<QzTask> queryByHouseNumber(String houseNumber, Integer status) {
//        return qzTaskDao.queryByHouseNumber(houseNumber, status);
//    }
//
//    public QzTask queryByTaskId(Long taskId) {
//        return qzTaskDao.queryByTaskId(taskId);
//    }
//
//    //求助的新增
//    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
//    public int addHelp(String taskTitle, String taskTarget, String remark) {
//        var qzTak = new QzTask();
//        qzTak.setTaskId(IdWorker.getId());
//        qzTak.setTaskTitle(taskTitle);
//        qzTak.setRemark(remark);
//        qzTak.setTaskTarget(taskTarget);
//        qzTak.setTaskStatus(1);
//
//    /*long userId = Long.parseLong(JwtHelper.getUserId());
//    qzTak.setUserId(userId);
//    qzTak.setUserCreate(userId);*/
//        qzTak.setTimeCreate(new DateTime());
//        /* qzTak.setUserUpdate(userId);*/
//        qzTak.setTimeUpdate(new DateTime());
//        return qzTaskDao.insert(qzTak);
//    }
//
//
//    public List<QzTask> queryQzTask(Integer taskStatus) {
//        return qzTaskDao.queryQzTask(taskStatus);
//    }
}
