/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.party.member.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.Constants;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.service.IEnterpriseStaffService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.party.branch.entity.Branch;
import org.springblade.party.branch.service.IBranchService;
import org.springblade.system.orgposition.service.IOrgPositionService;
import org.springblade.utils.QueryWrapperUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.party.member.entity.Member;
import org.springblade.party.member.vo.MemberVO;
import org.springblade.party.member.wrapper.MemberWrapper;
import org.springblade.party.member.service.IMemberService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-03-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("party/member")
@Api(value = "", tags = "接口")
public class MemberController extends BladeController {

	private IMemberService memberService;
	private IBranchService branchService;
	private IOrgPositionService orgPositionService;
	private IEnterpriseStaffService enterpriseStaffService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入member")
	public R<MemberVO> detail(Member member) {
		Member detail = memberService.getOne(Condition.getQueryWrapper(member));
		return R.data(MemberWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入member")
	public R<IPage<MemberVO>> list(Member member, Query query, BladeUser bladeUser) {

		IPage<Member> pages = memberService.page(Condition.getPage(query), null);
		return R.data(MemberWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入member")
	public R<IPage<MemberVO>> page(MemberVO member, Query query) {
		IPage<MemberVO> pages = memberService.selectMemberPage(Condition.getPage(query), member);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入member")
	public R save(@Valid @RequestBody Member member) {
		return R.status(memberService.save(member));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入member")
	public R update(@Valid @RequestBody Member member) {
		return R.status(memberService.updateById(member));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入member")
	public R submit(@Valid @RequestBody Member member) {

		return R.status(memberService.saveOrUpdate(member));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(memberService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 删除
	 */
	@GetMapping("/isPartyMember")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R isPartyMember(Long id) {
		List<Member> member = memberService.getBaseMapper().selectList(new QueryWrapper<>(new Member()).lambda().eq(Member::getPersonId,id));
		if(member.size()>0 ){
			return R.data(member.get(0));
		}else{
			return R.data(null);
		}
	}


	/**
	 * 园区小程序申请党员
	 */
	@PostMapping("/apply")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "申请", notes = "传入member")
	public R apply(@Valid @RequestBody Member member) {
		member.setAuditStatus(0);
		Assert.notNull(member.getPersonType(),"人员类型不能位空");
		if(member.getPersonType() == Constants.PARTY_MEMBER_TYPE_ENTERPRISESTAFF){//员工
			EnterpriseStaff enterpriseStaff = enterpriseStaffService.getById(member.getPersonId());
			Assert.notNull(enterpriseStaff,"找不到对应的员工信息");
			member.setIdCard(member.getIdCard());
			member.setSex(enterpriseStaff.getSex());
			member.setName(enterpriseStaff.getRealName());
			member.setPhone(enterpriseStaff.getPhone());

		}

		return R.status(memberService.save(member));
	}

}
