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.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.finance.entity.ChannelSpidView;
import org.springblade.finance.entity.DealUserAccounts;
import org.springblade.finance.entity.DealUsersView;
import org.springblade.finance.enums.DealTypeEnum;
import org.springblade.finance.query.ChannelSpidPageQuery;
import org.springblade.finance.service.ChannelSpidViewService;
import org.springblade.finance.service.DealUserAccountsService;
import org.springblade.finance.service.IDealUsersViewService;
import org.springblade.finance.service.IYksytChannelSpidService;
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;

/**
 * <p>
 * 其它结算渠道产品对照表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2022-04-07
 */
@RestController
@RequestMapping("finance/yksytChannelSpid")
@Api(value = "优酷收银台渠道产品", tags = "优酷收银台渠道产品接口")
public class YksytChannelSpidController {


	@Autowired
	private ChannelSpidViewService channelSpidViewService;

	@Autowired
	private IYksytChannelSpidService yksytChannelSpidService;

	@Autowired
	private DealUserAccountsService dealUserAccountsService;

	@Autowired
	private IDealUsersViewService dealUsersViewService;

	@Autowired
	private UserClient userService;


	/**
	 * 渠道列表
	 * @return
	 */
	@GetMapping("/selectList")
	@ApiOperation("渠道下拉列表列表")
	public R list(){
		QueryWrapper<DealUsersView> queryWrapper = new QueryWrapper<DealUsersView>();
		queryWrapper.select("DEAL_USERID, DEAL_USER_NAME").isNotNull("DEAL_USERID");
		queryWrapper.eq("MODULE", DealTypeEnum.qtqd_deal.getValue());
		List<DealUsersView> list = dealUsersViewService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("dealUserid", obj.getDealUserid());
				map.put("dealUserName", obj.getDealUserName());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}


	@GetMapping("/interestSelectList")
	@ApiOperation("渠道下拉列表列表-会员权益分类单独数据")
	public R interestSelectList(){
		QueryWrapper<DealUsersView> queryWrapper = new QueryWrapper<DealUsersView>();
		queryWrapper.select("DEAL_USERID, DEAL_USER_NAME").isNotNull("DEAL_USERID");
		queryWrapper.eq("MODULE", DealTypeEnum.hyqy_deal.getValue());
		List<DealUsersView> list = dealUsersViewService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("dealUserid", obj.getDealUserid());
				map.put("dealUserName", obj.getDealUserName());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}



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


	/**
	 * @return
	 */
	@GetMapping("/spidList")
	@ApiOperation(value = "其他渠道结算->产品下拉列表")
	@ApiImplicitParam(name = "dealUserIds", value = "结算用户ID，逗号隔开。全部渠道传空或者不传递", required = false)
	public R spidList(String[] dealUserIds){
		QueryWrapper<ChannelSpidView> 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<ChannelSpidView> list = channelSpidViewService.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);
	}

	/**
	 * @return
	 */
	@GetMapping("/companyList")
	@ApiOperation(value = "公司列表")
	@ApiImplicitParam(name = "channelIds", value = "渠道ID，逗号隔开。全部渠道传空或者不传递", required = false)
	public R<List<ChannelSpidView>> companyList(String[] channelIds){
		LambdaQueryWrapper<ChannelSpidView> queryWrapper = Wrappers.<ChannelSpidView>lambdaQuery();
		if(channelIds != null && channelIds.length > 0) {
			queryWrapper.in(ChannelSpidView::getChannelId, Arrays.asList(channelIds));
		}
		queryWrapper.eq(ChannelSpidView::getDealType,2);
		queryWrapper.select(ChannelSpidView::getCompany)
			.groupBy(ChannelSpidView::getCompany);
		return R.data(channelSpidViewService.list(queryWrapper));
	}


	@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 = yksytChannelSpidService.getPageByQuery(page, collect);
		for (ChannelSpidVO record : pages.getRecords()) {
			record.setInputName(getUserName(record.getInputter()));
			record.setCheckName(getUserName(record.getChecker()));
		}
		return R.data(pages);
	}


	@GetMapping("/interestPage")
	@ApiOperation(value = "渠道产品分页-会员权益分类单独数据")
	public R<IPage<ChannelSpidVO>> interestPage(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 = yksytChannelSpidService.getInterestPageByQuery(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){
		if(StringUtils.isBlank(userId)){
			return null;
		}
		UserModel byId = userService.getById(Long.valueOf(userId));
		if(byId != null){
			return byId.getRealName();
		}
		return null;
	}


}

