package com.jsowell.web.controller.pile;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.jsowell.common.annotation.Log;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.core.page.TableDataInfo;
import com.jsowell.common.enums.BusinessType;
import com.jsowell.common.enums.uniapp.BalanceChangesEnum;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.MemberBasicInfo;
import com.jsowell.pile.domain.MemberPlateNumberRelation;
import com.jsowell.pile.dto.UniAppQueryMemberBalanceDTO;
import com.jsowell.pile.service.IMemberBasicInfoService;
import com.jsowell.pile.service.IMemberPlateNumberRelationService;
import com.jsowell.pile.service.IMemberTransactionRecordService;
import com.jsowell.pile.vo.uniapp.MemberVO;
import com.jsowell.pile.vo.uniapp.MemberWalletLogVO;
import com.jsowell.pile.vo.web.MemberTransactionVO;
import com.jsowell.pile.vo.web.UpdateMemberBalanceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员基础信息Controller
 *
 * @author jsowell
 * @date 2022-10-12
 */
@RestController
@RequestMapping("/member/info")
public class MemberBasicInfoController extends BaseController {
	@Autowired
	private IMemberBasicInfoService memberBasicInfoService;

	@Autowired
	private IMemberTransactionRecordService memberTransactionRecordService;

	@Autowired
	private IMemberPlateNumberRelationService memberPlateNumberRelationService;
	/**
	 * 查询会员基础信息列表
	 */
	@PreAuthorize("@ss.hasPermi('member:info:list')")
	@GetMapping("/list")
	public TableDataInfo list(MemberBasicInfo memberBasicInfo) {
		startPage();
		List<MemberVO> list = memberBasicInfoService.selectMemberList(memberBasicInfo.getMobileNumber(), memberBasicInfo.getNickName());
		return getDataTable(list);
	}

	/**
	 * 导出会员基础信息列表
	 */
	/*@PreAuthorize("@ss.hasPermi('member:info:export')")
	@Log(title = "会员基础信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, MemberBasicInfo memberBasicInfo) {
		List<MemberBasicInfo> list = memberBasicInfoService.selectMemberBasicInfoList(memberBasicInfo);
		ExcelUtil<MemberBasicInfo> util = new ExcelUtil<MemberBasicInfo>(MemberBasicInfo.class);
		util.exportExcel(response, list, "会员基础信息数据");
	}*/

	/**
	 * 获取会员基础信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('member:info:query')")
	@GetMapping(value = "/{memberId}")
	public AjaxResult getInfo(@PathVariable("memberId") String memberId) {
		return AjaxResult.success(memberBasicInfoService.queryMemberInfoByMemberId(memberId));
	}

	@PreAuthorize("@ss.hasPermi('member:info:query')")
	@GetMapping(value = "/getMemberPersonPileInfo/{memberId}")
	public AjaxResult getMemberPersonPileInfo(@PathVariable("memberId") String memberId){
		return AjaxResult.success(memberBasicInfoService.getMemberPersonPileInfo(memberId));
	}

	/**
	 * 新增会员基础信息
	 */
	// @PreAuthorize("@ss.hasPermi('member:info:add')")
	// @Log(title = "会员基础信息", businessType = BusinessType.INSERT)
	// @PostMapping
	// public AjaxResult add(@RequestBody MemberBasicInfo memberBasicInfo) {
	// 	return toAjax(memberBasicInfoService.insertMemberBasicInfo(memberBasicInfo));
	// }

	/**
	 * 修改会员基础信息
	 */
	@PreAuthorize("@ss.hasPermi('member:info:edit')")
	@Log(title = "会员基础信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MemberBasicInfo memberBasicInfo) {
		return toAjax(memberBasicInfoService.updateMemberBasicInfo(memberBasicInfo));
	}

	/**
	 * 删除会员基础信息
	 */
	@PreAuthorize("@ss.hasPermi('member:info:remove')")
	@Log(title = "会员基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Integer[] ids) {
		return toAjax(memberBasicInfoService.deleteMemberBasicInfoByIds(Lists.newArrayList(ids)));
	}

	/**
	 * 充值/扣款余额
	 */
	@PreAuthorize("@ss.hasPermi('member:balance:update')")
	@Log(title = "会员充值/扣款余额", businessType = BusinessType.UPDATE)
	@PutMapping("/updateGiftBalance")
	public AjaxResult updateGiftBalance(@RequestBody UpdateMemberBalanceDTO dto) {
		logger.info("后管充值/扣款余额 param:{}", dto.toString());
		// 判断入参
		return toAjax(memberBasicInfoService.updateMemberBalance(dto));
	}

	/**
	 * 查询会员钱包流水
	 */
	@PostMapping("/queryMemberBalanceChanges")
	public TableDataInfo queryMemberBalanceChanges(@RequestBody UniAppQueryMemberBalanceDTO dto) {
		String type = dto.getType();
		if (!StringUtils.equals("1", type) && !StringUtils.equals("2", type)) {
			type = "";
		}
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		List<MemberWalletLogVO> list = memberBasicInfoService.getMemberBalanceChanges(dto.getMemberId(), type);
		for (MemberWalletLogVO walletLogVO : list) {
			String subType = walletLogVO.getSubType();
			String subTypeValue = BalanceChangesEnum.getValueByCode(subType);
			if (StringUtils.isNotBlank(subTypeValue)) {
				walletLogVO.setSubType(subTypeValue);
			}
		}
		return getDataTable(list);
	}

	/**
	 * 查询会员交易流水
	 * IMemberTransactionRecordService
	 */
	@PostMapping("/selectMemberTransactionRecordList")
	public TableDataInfo selectMemberTransactionRecordList(@RequestBody UniAppQueryMemberBalanceDTO dto) {
		startPage();
		List<MemberTransactionVO> list = memberTransactionRecordService.selectMemberTransactionRecordList(dto.getMemberId());
		return getDataTable(list);
	}

	@PreAuthorize("@ss.hasPermi('member:info:edit')")
	@Log(title = "修改车牌号信息", businessType = BusinessType.UPDATE)
	@PostMapping("/updatePlateNumber")
	public AjaxResult updatePlateNumber(@RequestBody MemberPlateNumberRelation relation){
		int i = memberPlateNumberRelationService.updatePlateNumber(relation);
		logger.info("后管修改用户：{} 的车牌号：{}", relation.getMemberId(), relation.getLicensePlateNumber());
		return toAjax(i);
	}

	/**
	 * 删除会员绑定的车牌号信息
	 */
	@PreAuthorize("@ss.hasPermi('member:info:remove')")
	@Log(title = "会员绑定的车牌号信息", businessType = BusinessType.DELETE)
	@PostMapping("/deletePlateNumber")
	public AjaxResult deletePlateNumber(@RequestBody MemberPlateNumberRelation relation){
		int i = memberPlateNumberRelationService.unbindingPlateNumber(relation.getMemberId(), relation.getLicensePlateNumber());
		logger.info("后管解除绑定用户：{} 的车牌号：{}", relation.getMemberId(), relation.getLicensePlateNumber());
		return toAjax(i);
	}
}
