package com.jeequan.jeepay.mgr.ctrl.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.SysAdvertConfig;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.SysAdvertConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统广告配置
 *
 * @author xiaoyu
 * 
 * @date 2023/2/20 10:57
 */
@Slf4j
@RestController
@RequestMapping("/api/advert")
public class SysAdvertController extends CommonCtrl {

	@Autowired private SysAdvertConfigService advertConfigService;


	/**
	 * @author: xiaoyu
	 * @date: 2023/2/20 11:06
	 * @describe: 广告配置列表
	 */
	@PreAuthorize("hasAnyAuthority('ENT_ADVERT_LIST', 'ENT_ADVERT_VIEW')")
	@GetMapping
	public ApiRes pages() {
		SysAdvertConfig record = getObject(SysAdvertConfig.class);

		LambdaQueryWrapper<SysAdvertConfig> condition = SysAdvertConfig.gw();
		condition.eq(record.getAdvertId() != null, SysAdvertConfig::getAdvertId, record.getAdvertId());
		condition.eq(record.getReleaseState() != null, SysAdvertConfig::getReleaseState, record.getReleaseState());
		condition.eq(record.getAdvertType() != null, SysAdvertConfig::getAdvertType, record.getAdvertType());
		condition.eq(record.getAppPlace() != null, SysAdvertConfig::getAppPlace, record.getAppPlace());
		condition.eq(record.getAppPlaceType() != null, SysAdvertConfig::getAppPlaceType, record.getAppPlaceType());
		condition.like(StringUtils.isNotEmpty(record.getTitle()), SysAdvertConfig::getTitle, record.getTitle());
		condition.eq(StringUtils.isNotEmpty(record.getMchNo()), SysAdvertConfig::getMchNo, record.getMchNo());
		condition.orderByDesc(SysAdvertConfig::getCreatedAt);
		IPage<SysAdvertConfig> pages = advertConfigService.page(getIPage(), condition);

		for (SysAdvertConfig config: pages.getRecords()) {
			if (StringUtils.isNotEmpty(config.getMchNo())) {
				MchInfo mchInfo = mchInfoService.getById(config.getMchNo());
				if (mchInfo != null) {
					config.addExt("infoName", mchInfo.getMchName());
				}else {
					config.addExt("infoName", "");
				}
			}
		}

		//返回数据
		return ApiRes.ok(pages);
	}

	/** detail */
	@PreAuthorize("hasAuthority( 'ENT_ADVERT_VIEW' )")
	@RequestMapping(value="/{advertId}", method = RequestMethod.GET)
	public ApiRes getConfigs(@PathVariable("advertId") Long advertId) {
		return ApiRes.ok(advertConfigService.getById(advertId));
	}

	/**
	 * @author: xiaoyu
	 * @date: 2023/2/20 11:06
	 * @describe: 广告配置修改
	 */
	@PreAuthorize("hasAnyAuthority('ENT_ADVERT_EDIT')")
	@MethodLog(remark = "广告配置修改")
	@RequestMapping(value="/{advertId}", method = RequestMethod.PUT)
	public ApiRes update(@PathVariable("advertId") Long advertId) {
		SysAdvertConfig record = getObject(SysAdvertConfig.class);
		record.setAdvertId(advertId);
		if (getReqParamJSON().getByte("state") != null) {
			record.setReleaseState(getReqParamJSON().getByte("state"));
		}

		advertConfigService.updateAdvert(record);
		return ApiRes.ok();
	}

	/**
	 * @author: xiaoyu
	 * @date: 2023/2/20 11:10
	 * @describe: 广告配置新增
	 */
	@PreAuthorize("hasAuthority( 'ENT_ADVERT_ADD' )")
	@MethodLog(remark = "广告配置新增")
	@RequestMapping(value="", method = RequestMethod.POST)
	public ApiRes add() {
		JSONObject paramJSON = getReqParamJSON();
		SysAdvertConfig record = getObject(SysAdvertConfig.class);
		String infoIds = paramJSON.getString("infoIds");
		record.setCreatedUid(getCurrentUser().getSysUserId());
		record.setCreatedBy(getCurrentUser().getSysUser().getRealname());
		advertConfigService.addConfig(infoIds, record);
		return ApiRes.ok();
	}

	/** delete */
	@PreAuthorize("hasAuthority('ENT_ADVERT_DEL')")
	@MethodLog(remark = "广告配置删除")
	@DeleteMapping
	public ApiRes del() {
		String delAdvertIds = getValStringRequired("delAdvertIds");
		List<String> advertIdList = Arrays.stream(delAdvertIds.split(",")).collect(Collectors.toList());
		if(advertConfigService.removeByIds(advertIdList)) {
			return ApiRes.ok();
		}
		return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_DELETE);
	}

}
