package com.fs.his.controller;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserIntegralLogs;
import com.fs.his.domain.Tlintegral;
import com.fs.his.dto.ExpressNotifyDTO;
import com.fs.his.dto.ExpressResultDTO;
import com.fs.his.mapper.FsUserIntegralLogsMapper;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.service.IFsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

import static com.fs.his.utils.PhoneUtil.encryptPhone;


@Api("公共接口")
@RestController
@RequestMapping(value="/app/common")
public class FsExpressCommonController extends BaseController {
	@Autowired
	IFsStoreOrderService orderService;
	@Autowired
	IFsUserService userService;
	@Autowired
	private FsUserIntegralLogsMapper integralLogsMapper;
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@ApiOperation("查询运单信息")
	@PostMapping(path = "/expressNotify" )
	public ExpressResultDTO expressNotify(ExpressNotifyDTO notifyDTO) throws Exception
	{
		return orderService.updateDeliveryItem(notifyDTO);
	}
	@PostMapping(path = "/integral" )
	public R Integral(@RequestBody String json) throws Exception
	{
		logger.info("螳螂积分："+json);
		Tlintegral tl = JSON.parseObject(json, Tlintegral.class);
		if (tl.getTotalPoints()==null||tl.getIncrPoints()==null){
			return R.ok();
		}


		FsUser user=userService.selectFsUserByPhone(tl.getPhoneNum());
		if (user==null){
			user=userService.selectFsUserByPhone(tl.getPhoneNum());
		}
		if(user!=null){
			if (user.getIntegralStatus()==0){
				FsUser userMap=new FsUser();
				userMap.setUserId(user.getUserId());
				Long integral = user.getIntegral();
				userMap.setIntegral(integral+tl.getTotalPoints());
				userMap.setUpdateTime(new DateTime());
				userMap.setIntegralStatus(1);
				userService.updateFsUser(userMap);
				FsUserIntegralLogs logs = new FsUserIntegralLogs();
				logs.setIntegral(tl.getTotalPoints());
				logs.setUserId(userMap.getUserId());
				logs.setBalance(userMap.getIntegral());
				logs.setLogType(8);
				logs.setCreateTime(new Date());
				integralLogsMapper.insertFsUserIntegralLogs(logs);
			}else {
				FsUser userMap=new FsUser();
				userMap.setUserId(user.getUserId());
				Long integral = user.getIntegral();
				userMap.setIntegral(integral+tl.getIncrPoints());
				userMap.setUpdateTime(new DateTime());
				userService.updateFsUser(userMap);
				FsUserIntegralLogs logs = new FsUserIntegralLogs();
				logs.setIntegral(tl.getIncrPoints());
				logs.setUserId(userMap.getUserId());
				logs.setBalance(userMap.getIntegral());
				logs.setLogType(8);
				logs.setCreateTime(new Date());
				integralLogsMapper.insertFsUserIntegralLogs(logs);
			}

		} else{
			FsUser newUser=new FsUser();
			newUser.setPhone(tl.getPhoneNum());
			newUser.setNickName("微信用户"+tl.getPhoneNum().substring(tl.getPhoneNum().length()-4));
			newUser.setStatus(1);
			newUser.setIntegralStatus(1);
			newUser.setIntegral(tl.getTotalPoints());
			newUser.setCreateTime(new Date());
			userService.insertFsUser(newUser);
			FsUserIntegralLogs logs = new FsUserIntegralLogs();
			logs.setIntegral(tl.getTotalPoints());
			logs.setUserId(newUser.getUserId());
			logs.setBalance(newUser.getIntegral());
			logs.setLogType(8);
			logs.setCreateTime(new Date());
			integralLogsMapper.insertFsUserIntegralLogs(logs);
		}
		return R.ok();
	}



}
