package com.td.boss.game.complayerland.service;

import com.td.boss.common.service.*;
import com.td.boss.game.complayerland.pojo.ComPlayerLand;
import com.td.boss.game.complayerland.pojo.ComPlayerLandAndCanSteal;
import com.td.boss.game.complayerland.vo.ComPlayerLandAndUserInfoVo;
import com.td.boss.game.complayerland.vo.ComPlayerLandVo;
import com.td.boss.game.complayerland.repository.ComPlayerLandRepository;
import com.td.boss.game.complayerprofit.service.ComPlayerProfitService;
import com.td.boss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
public class ComPlayerLandServiceImpl extends CommonServiceImpl<ComPlayerLandVo, ComPlayerLand, Integer> implements ComPlayerLandService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComPlayerLandRepository comPlayerLandRepository;

    @Autowired
    private ComPlayerProfitService comPlayerProfitService;


    @Override
    public List<ComPlayerLand> findAllByUserId(String userId) {
        return comPlayerLandRepository.findAllByUserId(userId);
    }


    @Override
    public List<ComPlayerLandAndUserInfoVo> findAllByCanStealNotSelf(String userId, Pageable pageable) {
        List<Object> objects = comPlayerLandRepository.findAllIsStealComPlayerLandAndUserInfo(userId, pageable);
        List<ComPlayerLandAndUserInfoVo> landAndUserInfoVos = new ArrayList<>();
        for (int i = 0; i < objects.size(); i++) {
            Object[] tempObj = (Object[]) objects.get(i);
            landAndUserInfoVos.add(new ComPlayerLandAndUserInfoVo((String) tempObj[0], (Boolean) tempObj[1], (Date) tempObj[2], (Integer) tempObj[3], (Date) tempObj[4], (String) tempObj[5]));
        }
        return landAndUserInfoVos;
    }

    @Override
    public List<ComPlayerLandAndCanSteal> findCanStealByUserIdAndOtherUserId(String userId, String otherUserId) {
        String sql = "SELECT\n" +
                "a.*,\n" +
                "(CASE WHEN c.id IS NULL THEN 1 ELSE 0 END ) AS can_steal\n" +
                "FROM\n" +
                "com_player_land AS a \n" +
                "LEFT JOIN com_player_profit AS c ON a.user_id = c.target_id AND c.user_id = " + userId + " AND a.config_land_id = c.land_id AND (( ISNULL(a.plant_flag) = 1 AND c.plant_flag = CONCAT(a.user_id,'or',a.config_land_id)) OR a.plant_flag = c.plant_flag )\n" +
                "WHERE a.user_id = " + otherUserId + " AND is_lease = 1 AND is_plant = 1 AND DATE_ADD(a.plant_start,INTERVAL a.plant_mature DAY) <= NOW()\n" +
                "AND a.lease_time > NOW()";
        Query nativeQuery = em.createNativeQuery(sql, ComPlayerLandAndCanSteal.class);
        List list = nativeQuery.getResultList();
        return list;
    }

    @Override
    public List<ComPlayerLandAndUserInfoVo> findAllByCanStealAndOtherUser(String userId) {
        return null;
    }

    @Override
    public List<ComPlayerLand> findAllByCanStealOtherLands(String userId, String otherUserId) {
        //查找目标
        List<ComPlayerLand> comPlayerLandList = comPlayerLandRepository.findAllByUserIdAndIsLeaseAndIsPlant(otherUserId, 1, 1).stream()
                .filter(
                        e ->
                                //过滤掉对应的条件
                                //过滤掉未成熟的
                                DateUtil.getOldDateAddDay(e.getPlantStart(), e.getPlantMature()).getTime() <= DateUtil.getNowDate().getTime()
                                        &&
                                        //判断是否偷取过目标的土地
                                        comPlayerProfitService.findByUserIdAndTargetIdAndLandIdAndPlantFlag(userId, e.getUserId(), e.getConfigLandId(), e.getPlantFlag()) == null

                )
                .collect(Collectors.toList());
        return comPlayerLandList;
    }

    @Override
    public ComPlayerLand findById(Integer id) {
        Optional<ComPlayerLand> comPlayerLand = comPlayerLandRepository.findById(id);

        return comPlayerLand.orElse(null);
    }

    @Override
    public ComPlayerLand findByLandId(Integer id) {
        Optional<ComPlayerLand> comPlayerLand = comPlayerLandRepository.findByConfigLandId(id);

        return comPlayerLand.orElse(null);
    }


    @Override
    public ComPlayerLand findByUserIdAndLandId(String userId, Integer id) {
        return comPlayerLandRepository.findByUserIdAndConfigLandId(userId, id).orElse(null);
    }

    @Override
    public ComPlayerLand AddComPlayerLand(ComPlayerLand comPlayerLand) {
        return comPlayerLandRepository.save(comPlayerLand);
    }


}
