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.constants.ProjectConstant;
import cn.pluss.platform.entity.Fans;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantOrder;
import cn.pluss.platform.fans.FansService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.PageFansVO;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/fans")
@Transactional
@Api(value = "Fans控制类", tags = { "Fans接口" })
public class FansController {

	@Resource
	private FansService fansService;
	
	@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 = "merchantCode", dataType = "String", required = true), })
	public Result<PageFansVO> list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size, String merchantCode,String startTime ,String endTime) {
		PageFansVO pageVO = new PageFansVO();
		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("merchantCode", merchantCode);
		map.put("offset", (page - 1) * size);
		try {
			if(StringUtil.isNotEmpty(startTime)) {
				map.put("startTime", sdf.parse(startTime));
			}
			if(StringUtil.isNotEmpty(endTime)) {
				map.put("endTime", sdf.parse(endTime));
			}
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		List<Fans> fansList = fansService.queryFansPage(map);
		for (Fans fans : fansList) {
			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();
				}
			}

			Fans.updateOrderTypeDesc(fans);
		}
		Integer count = fansService.queryFansPageCount(map);
		// 返回参数
		PageInfo<Fans> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count,size), page.longValue(), size.longValue(), fansList);
		pageVO.setPageInfo(pager);
		pageVO.setFansCount(count);
		Double fansDealMoney = fansService.sumConsumMoneyByMerchantCode(map);
		pageVO.setFansDealMoney(fansDealMoney);
		return ResultGenerator.genSuccessResult(pageVO);
	}

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

	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true) })
	public Result delete(@PathVariable Integer id) {
		Fans fans = new Fans();
		fans.setId(id);
		fansService.deleteFans(fans);
		return ResultGenerator.genSuccessResult();
	}

	@PutMapping
	@ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Fans", value = "更新的Fans实例", paramType = "body", dataType = "Fans", required = true) })
	public Result update(@RequestBody Fans fans) {
		fansService.updateFans(fans);
		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) {
		Fans fans = new Fans();
		fans.setId(id.intValue());
		fans = fansService.queryFans(fans);
		return ResultGenerator.genSuccessResult(fans);
	}

}
