package com.cpop.system.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cpop.api.cloudDb.core.dto.CloudStoreDto;
import com.cpop.api.cloudDb.handler.CloudStoreHandler;
import com.cpop.api.tencent.location.handler.TencentLocationHandler;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.SqlUtils;
import com.cpop.core.utils.bean.BeanUtils;
import com.cpop.system.business.bo.StoreBo;
import com.cpop.system.business.entity.Brand;
import com.cpop.system.business.entity.Store;
import com.cpop.system.business.mapper.StoreMapper;
import com.cpop.system.business.service.BrandService;
import com.cpop.system.business.service.StoreService;
import com.cpop.system.business.vo.StorePageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowKey;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.StoreSignTableDef.STORE_SIGN;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;

/**
 * 系统-店铺/校区表 服务层实现。
 *
 * @author DB
 * @since 2023-10-26
 */
@Service("storeService")
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

	/**
	 * 查询校区分页列表
	 * @param name 请求参数
	 * @return R<PageVo<CampusPageListVo>>
	 * @author Administrator
	 * @since 2023/6/7 0007 10:18
	 */
	@Override
	public Page<StorePageVo> getStorePage(String name) {
		PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
		return this.getMapper().paginateAs(Page.of(pageDomain.getPageNum(), pageDomain.getPageSize()), QueryWrapper.create()
						.select(STORE.ID, STORE.STORE_NAME, STORE.CREATE_TIME, STORE.STORE_ADDR, STORE.PERSON_CHARGE_NAME.as(StorePageVo::getPersonCharge),
								STORE.PERSON_CHARGE_PHONE.as(StorePageVo::getPhone), STORE.WX_MCH_ID)
						.select(STORE_SIGN.EXPIRE_DATE)
						.select(BRAND.BRAND_NAME)
						.from(STORE)
						.leftJoin(STORE_SIGN).on(STORE_SIGN.STORE_ID.eq(STORE.ID))
						.leftJoin(BRAND).on(BRAND.ID.eq(STORE.BRAND_ID))
						.and(STORE.STORE_NAME.like(name))
						.orderBy(STORE.CREATE_TIME.desc()),
				StorePageVo.class);
	}

	/**
	 * 更新校区
	 * @author DB
	 * @since 2024/1/15
	 * @param bo 请求参数
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateStore(StoreBo bo) {
		Store store = BeanUtils.mapToClass(bo, Store.class);
		this.updateById(store);
	}
}
