package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.cash.CashService;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.fans.FansService;
import cn.pluss.platform.fansConsumReturn.FansConsumReturnService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.FansSumRewardsInfoVO;
import cn.pluss.platform.vo.PageFansConsumReturnVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fansConsumReturn")
@Transactional
@Api(value = "FansConsumReturn控制类", tags = { "FansConsumReturn接口" })
public class FansConsumReturnController {

	@Resource
	private FansConsumReturnService fansConsumReturnService;
	@Resource
	private FansService fansService;

	@Autowired
	private UserAppService uaService;

	@Resource
	private CashService cashService;
	
	@Resource
	private MerchantBaseInfoService merchantBaseInfoService;
	@Resource
	private MerchantOrderService merchantOrderService;

	@GetMapping
	@ApiOperation(tags = {"页面-粉丝_粉丝消费"}, value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
			@ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0"),
			@ApiImplicitParam(name = "merchantCode", value = "商家Code", paramType = "query", dataType = "String", required = true), })
	public Result<PageFansConsumReturnVO> list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size, String merchantCode,String startTime ,String endTime) {
		UserApp userApp = uaService.queryUserAppByToken();
		merchantCode = userApp.getMerchantCode();

		PageFansConsumReturnVO pageVO = new PageFansConsumReturnVO();
		if (size == null || size == 0) {
			size = ProjectConstant.PAGE_SIZE;
		}
		if (page == null || page == 0) {
			page = 1;
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", size);
		map.put("offset", (page - 1) * size);
		map.put("lastMerchantCode", merchantCode);
		try {
			if(StringUtil.isNotEmpty(startTime)) {
				map.put("startTime", sdf.parse(startTime));
			}
			if(StringUtil.isNotEmpty(endTime)) {
				map.put("endTime", sdf.parse(endTime));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Double fansConsumMonyNowDayCount=fansService.queryFansSumConsumMoney(map);//消费金额
		if(fansConsumMonyNowDayCount==null) {
			fansConsumMonyNowDayCount=0d;
		}
		Double fansReturnMoneyNowDayCount=fansService.queryFansSumShareMoney(map);//奖励金额
		if(fansReturnMoneyNowDayCount==null) {
			fansReturnMoneyNowDayCount=0d;
		}
		
		List<Fans> fansConsumReturnList = fansService.queryFansPage(map);
		for (Fans fans : fansConsumReturnList) {
			MerchantOrder merchantOrder=new MerchantOrder();
			merchantOrder.setOrderNumber(fans.getOrderNumber());
			List<MerchantOrder> merchantOrderList=merchantOrderService.list(new QueryWrapper<>(merchantOrder));
			if(merchantOrderList!=null&&merchantOrderList.size()>0) {
				MerchantBaseInfo merchantBaseInfo=new MerchantBaseInfo();
				merchantBaseInfo.setMerchantCode(merchantOrderList.get(0).getMerchantCode());
				merchantBaseInfo=merchantBaseInfoService.queryMerchantBaseInfo(merchantBaseInfo);
				if(null!=merchantBaseInfo&&StringUtil.isNotEmpty(merchantBaseInfo.getAlias())) {
					fans.setAlias(merchantBaseInfo.getAlias());
				}
			}
			if(!StringUtil.isEmpty(fans.getNickName())) {
				try {
					fans.setNickName(URLDecoder.decode(fans.getNickName(), "UTF-8"));
					fans.setNickName(URLDecoder.decode(fans.getNickName(), "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		Integer count = fansService.queryFansPageCount(map);
		// 返回参数
		PageInfo<Fans> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count,size), page.longValue(), size.longValue(), fansConsumReturnList);
		pageVO.setPageInfo(pager);
//		FansInfoCountVO fansInfoCountVO = fansConsumReturnService.sumFansConsumReturnInfo(merchantCode);
//		if (fansInfoCountVO != null) {
		pageVO.setFansConsumMonyNowDayCount(StringUtil.bigDecimal(fansConsumMonyNowDayCount));
		pageVO.setFansReturnMoneyNowDayCount(StringUtil.bigDecimal(fansReturnMoneyNowDayCount));
//		}
		return ResultGenerator.genSuccessResult(pageVO);
	}
	

	@PostMapping
	@ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "FansConsumReturn", value = "待添加的FansConsumReturn实例", paramType = "body", dataType = "FansConsumReturn", required = true) })
	public Result add(@RequestBody FansConsumReturn fansConsumReturn) {
		fansConsumReturnService.saveFansConsumReturn(fansConsumReturn);
		return ResultGenerator.genSuccessResult();
	}

	@PutMapping
	@ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "FansConsumReturn", value = "更新的FansConsumReturn实例", paramType = "body", dataType = "FansConsumReturn", required = true) })
	public Result update(@RequestBody FansConsumReturn fansConsumReturn) {
		fansConsumReturnService.updateFansConsumReturn(fansConsumReturn);
		return ResultGenerator.genSuccessResult();
	}

	@GetMapping("/{id}")
	@ApiOperation(value = "获取单个值", notes = "查看单个项目的内容", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "查询的id", paramType = "path", required = true, dataType = "Integer", defaultValue = "0") })
	public Result detail(@PathVariable Long id) {
		FansConsumReturn fansConsumReturn = new FansConsumReturn();
		fansConsumReturn.setId(id.intValue());
		fansConsumReturn = fansConsumReturnService.queryFansConsumReturn(fansConsumReturn);
		return ResultGenerator.genSuccessResult(fansConsumReturn);
	}

	@GetMapping("/rewards")
	@ApiOperation(tags = {"页面-粉丝_粉丝消费", "页面-粉丝_粉丝奖励"}, value = "获取粉丝奖励数据", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "merchantCode", value = "商家Code", paramType = "merchantCode", dataType = "String", required = true), })
	public Result<FansSumRewardsInfoVO> listRewards(String merchantCode) {
		UserApp tokenUa = uaService.queryUserAppByToken();
		merchantCode = tokenUa.getMerchantCode();
		Map<String, Object> map= new HashMap<>();
		map.put("merchantCode", merchantCode);
		FansSumRewardsInfoVO fVO = new FansSumRewardsInfoVO();
		Double sumFansAllowCashMoney = fansConsumReturnService.sumFansAllowCashMoney(map);//可提现总金额
		Double sumSuccessCash = cashService.sumFansSuccessCash(map);//已经被提现的金额
		Double allowCashMoney = StringUtil.bigDecimal(sumFansAllowCashMoney - sumSuccessCash);//可提现金额
		Double sumAreadlyCash = cashService.sumFansAlreadyCash(map);//已经提现金额
//		Double noCashMoney = fansConsumReturnService.sumFansNotAllowCashMoney(map);//不可提现金额
		Double sumAllShareMoney = fansConsumReturnService.sumAllShareMoney(map);//累计分润
		map.put("startTime", DateUtils.getDayBegin());
		map.put("endTime", DateUtils.getDayEnd());
		Double sumNowDayShareMoney = fansConsumReturnService.sumNowDayShareMoney(map);// 今日分润
		Double noCashMoney=sumNowDayShareMoney;
		fVO.setAllowCashMoney(allowCashMoney);
		fVO.setNoCashMoney(noCashMoney);
		fVO.setSumAllShareMoney(sumAllShareMoney);
		fVO.setSumAreadlyCash(sumAreadlyCash);
		fVO.setSumNowDayShareMoney(sumNowDayShareMoney);
		return ResultGenerator.genSuccessResult(fVO);
	}

}
