package com.cool.store.dao.decoration;

import com.cool.store.dao.HyOpenAreaInfoDAO;
import com.cool.store.dto.decoration.DecorationListDTO;
import com.cool.store.entity.HyOpenAreaInfoDO;
import com.cool.store.entity.decoration.ShopDecorationAssignDO;
import com.cool.store.mapper.decoration.ShopDecorationAssignMapper;
import com.cool.store.request.decoration.DecorationListRequest;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/10/29 15:07
 * @Version 1.0
 */
@Repository
public class ShopDecorationAssignDAO {

    @Resource
    private ShopDecorationAssignMapper shopDecorationAssignMapper;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;


    public Integer insert(ShopDecorationAssignDO shopDecorationAssignDO) {
        if (shopDecorationAssignDO == null){
            return 0;
        }
        return shopDecorationAssignMapper.insert(shopDecorationAssignDO);
    }

    public ShopDecorationAssignDO getById(Long id) {
        if (id == null){
            return null;
        }
        return shopDecorationAssignMapper.selectByPrimaryKey(id);
    }

    public Integer update(ShopDecorationAssignDO shopDecorationAssignDO) {
        if (shopDecorationAssignDO == null){
            return 0;
        }
        return shopDecorationAssignMapper.updateByPrimaryKey(shopDecorationAssignDO);
    }

    public Integer countByTeamId(Long teamId) {
        if (teamId == null){
            return 0;
        }
        return shopDecorationAssignMapper.countByTeamId(teamId);
    }

    public List<DecorationListDTO> listByCondition(DecorationListRequest request) {
        if (request == null){
            return null;
        }
        if (request.getWantShopAreaId()!=null){
            HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(request.getWantShopAreaId());
            List<HyOpenAreaInfoDO> hyOpenAreaInfoDOList = hyOpenAreaInfoDAO.queryByKeyword(hyOpenAreaInfoDO.getAreaPath(), null, null, false);
            List<Long> wantShopAreaIds = hyOpenAreaInfoDOList.stream().map(HyOpenAreaInfoDO::getId).collect(Collectors.toList());
            request.setWantShopAreaIds(wantShopAreaIds);
        }
        return shopDecorationAssignMapper.listByCondition(request);
    }

    public ShopDecorationAssignDO getByShopId(Long shopId) {
        Example example = new Example(ShopDecorationAssignDO.class);
        example.createCriteria().andEqualTo("shopId", shopId);
        return shopDecorationAssignMapper.selectOneByExample(example);
    }
}
