package com.ssj.api.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ssj.api.domain.vo.merchant.ServiceVo;
import com.ssj.api.security.annotation.IgnoreSecurity;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.bean.weixin.libmy.domain.TbLibJoinConsume;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.sys.fx.service.MerchantService;
import com.ssj.service.weixin.library.service.ILibJoinConsumeService;

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

@Api(value = "商户相关的", tags = "商户相关的")
@RestController
@RequestMapping("/open/api/merchant")
public class ApiMerchantController extends BaseController {
	
	@Autowired
	private MerchantService merchantService;
	
	@Autowired
	private ILibJoinConsumeService  libJoinConsumeService;
	
	@IgnoreSecurity
	@RequestMapping(value = "/service")
    @ApiOperation(value = "获取商户套餐信息接口",  notes = "获取商户套餐信息接口")
    public ResponseEntity getAccessToken(@RequestBody ServiceVo requestVO) {
        ResponseEntity responseEntity = new ResponseEntity();
        Map<String,Object> data=new HashMap<String, Object>();
        try {
        	Merchant merchant=merchantService.getMerchantByLibId(requestVO.getLibId());
        	if(merchant!=null) {
        		
        		//大于2020-10-09才会结算。
        		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        		String dateString="2020-10-09";
        		
	        	if(requestVO.getType()==1 
	        			&& sdf.parse(dateString).getTime()<=new Date().getTime()) {
	        		
					merchant.setCurIsSummary(1);
					merchant.setCurSummaryTime(new Date());
					merchant.setNextSummaryTime(DateHelper.getMonthDate(merchant.getCurSummaryTime(),1));
					merchantService.save(merchant);
					
					TbLibJoinConsume consume = new TbLibJoinConsume();
					consume.setFromLibId(merchant.getLibId());
					consume.setFromMerchantId(merchant.getId());
					consume.setLibService(merchant.getLibService());
					consume.setPlatformService(0);
					consume.setType(2);
					consume.setRemarks("消费【新模式用户激活消费】 -来源开放接口");
					consume.setCreateTime(new Date());
					libJoinConsumeService.save(consume);
	        	}
	        	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        		data.put("id", merchant.getId());
        		data.put("libId", merchant.getLibId());
        		data.put("summaryTime", merchant.getCurSummaryTime()!=null ?  format.format(merchant.getCurSummaryTime()) : "");
        		data.put("endSummaryTime", merchant.getCurSummaryTime()!=null ? format.format(DateHelper.getMonthDate(merchant.getCurSummaryTime(),merchant.getLibServiceMonth())) : "");
        		data.put("signType", merchant.getSignType());
        		data.put("libService", merchant.getLibService());
	            responseEntity.success(data, "获取商户套餐信息成功");
        	}else {
        		responseEntity.failure(ResponseConstant.CODE_500, "libId未绑定商户");
        	}
        } catch (Exception e) {
            logger.error("获取商户套餐信息接口", e);
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }
	

	
}
