/**
 *
 */
package org.springblade.finance.controller;

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

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.finance.entity.ChannelSpid;
import org.springblade.finance.entity.DealUserAccounts;
import org.springblade.finance.query.ChannelSpidPageQuery;
import org.springblade.finance.service.ChannelSpidService;
import org.springblade.finance.service.DealUserAccountsService;
import org.springblade.finance.service.IIntChannelSpidService;
import org.springblade.finance.vo.ChannelSpidVO;
import org.springblade.system.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("finance/channelSpid")
@Api(value = "财务结算渠道产品", tags = "渠道产品接口")
public class ChannelSpidController extends BladeController {

	@Autowired
	private ChannelSpidService channelSpidService;

	@Autowired
	private IIntChannelSpidService intChannelSpidService;

	@Autowired
	private DealUserAccountsService dealUserAccountsService;

	@Autowired
	private UserClient userService;

	@GetMapping("/list")
	@ApiOperation(value = "渠道产品列表")
	@ApiImplicitParam(name = "channelIds", value = "渠道ID，逗号隔开。全部渠道传空或者不传递", required = false)
	public R<IPage<ChannelSpid>> list(String[] channelIds, Query query){
		LambdaQueryWrapper<ChannelSpid> queryWrapper = Wrappers.<ChannelSpid>lambdaQuery();
		if(channelIds != null && channelIds.length > 0) {
			queryWrapper.in(ChannelSpid::getChannelId, Arrays.asList(channelIds));
		}
		return R.data(channelSpidService.page(Condition.getPage(query), queryWrapper.orderByDesc(ChannelSpid::getAccountStartDate, ChannelSpid::getChannelId)));
	}

	@GetMapping("/spidList")
	@ApiOperation(value = "产品列表")
	@ApiImplicitParam(name = "dealUserIds", value = "结算用户ID，逗号隔开。全部渠道传空或者不传递", required = false)
	public R spidList(String[] dealUserIds){
		QueryWrapper<ChannelSpid> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("DISTINCT SPID,SHORTNAME").isNotNull("SPID");
		if(dealUserIds != null && dealUserIds.length > 0) {
			List<String> strings = Arrays.asList(dealUserIds);
			List<String> collect = null;
			if(strings != null && strings.size() > 0){
				QueryWrapper<DealUserAccounts> dealQueryWrapper = new QueryWrapper<DealUserAccounts>();
				dealQueryWrapper.select("DEAL_ACCOUNT_ID").isNotNull("DEAL_ACCOUNT_ID");
				dealQueryWrapper.in("DEAL_USERID",strings);
				List<DealUserAccounts> dealList = dealUserAccountsService.list(dealQueryWrapper);
				collect = dealList.stream().map(DealUserAccounts::getDealAccountId).collect(Collectors.toList());
			}
			queryWrapper.in("DEAL_ACCOUNT_ID", collect);
		}
		List<ChannelSpid> list = channelSpidService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("spid", obj.getSpid());
				map.put("shortname", obj.getShortname());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}


	@GetMapping("/page")
	@ApiOperation(value = "社会渠道->渠道产品分页")
	public R<IPage<ChannelSpidVO>> page(ChannelSpidPageQuery pageQuery, Query query) {
		if(query.getSize() == null){
			query.setSize(3);
		}
		String[] channels = null;
		if(StringUtils.isNotBlank(pageQuery.getDealUserIds())){
			channels = pageQuery.getDealUserIds().split(",");
		}
		List<String> strings = null;
		if(channels != null){
			strings = Arrays.asList(channels);
		}
		List<String> collect = null;
		if(strings != null && strings.size() > 0){
			QueryWrapper<DealUserAccounts> dealQueryWrapper = new QueryWrapper<DealUserAccounts>();
			dealQueryWrapper.select("DEAL_ACCOUNT_ID").isNotNull("DEAL_ACCOUNT_ID");
			dealQueryWrapper.in("DEAL_USERID",strings);
			List<DealUserAccounts> dealList = dealUserAccountsService.list(dealQueryWrapper);
			collect = dealList.stream().map(DealUserAccounts::getDealAccountId).collect(Collectors.toList());
		}
		IPage<ChannelSpidVO> page = Condition.getPage(query);
		IPage<ChannelSpidVO> pages = intChannelSpidService.getPageByQuery(page, collect);
		for (ChannelSpidVO record : pages.getRecords()) {
			record.setInputName(getUserName(record.getInputter()));
			record.setCheckName(getUserName(record.getChecker()));
		}
		return R.data(pages);
	}


	private String getUserName(String userId){
		UserModel byId = userService.getById(Long.valueOf(userId));
		if(byId != null){
			return byId.getRealName();
		}
		return null;
	}


}
