package org.springblade.party.feign;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.constant.Constants;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.enterprise.entity.Staff;
import org.springblade.enterprise.feign.IStaffClient;

import org.springblade.enterprise.vo.StaffVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.party.entity.PartyMember;
import org.springblade.party.vo.PartyMemberVO;
import org.springblade.party.wrapper.PartyMemberWrapper;
import org.springblade.party.service.IPartyMemberService;

import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-03-30
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/member")

public class PartyMemberClient implements IPartyMemberClient {

    private final IPartyMemberService memberService;
    private final IStaffClient staffClient;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public PartyMemberVO detail(PartyMember partyMember) {
        PartyMember detail = memberService.getOne(Condition.getQueryWrapper(partyMember));
        return PartyMemberWrapper.build().entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<PartyMemberVO> list(PartyMember partyMember) {
        return PartyMemberWrapper.build().listVO(memberService.list(Condition.getQueryWrapper(partyMember)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody PartyMember partyMember) {
        return memberService.save(partyMember);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody PartyMember partyMember) {
        return memberService.updateById(partyMember);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody PartyMember partyMember) {
        return memberService.saveOrUpdate(partyMember);
        }


    /**
     * 删除
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return memberService.deleteLogic(Func.toLongList(ids));
        }

	@Override
	@GetMapping("/isPartyMember")
	public R isPartyMember(Long id) {
		List<PartyMember> partyMember = memberService.getBaseMapper().selectList(new QueryWrapper<>(new PartyMember()).lambda().eq(PartyMember::getPersonId,id));
		if(partyMember.size()>0 ){
			return R.data(partyMember.get(0));
		}else{
			return R.data(null);
		}
	}

	@Override
	@PostMapping("/apply")
	public R apply(@Valid PartyMember partyMember) {
		partyMember.setAuditStatus(0);
		Assert.notNull(partyMember.getPersonType(),"人员类型不能位空");
		if(partyMember.getPersonType() == Constants.PARTY_MEMBER_TYPE_ENTERPRISESTAFF){//员工
			Staff enterpriseStaff = new Staff();
			enterpriseStaff.setId(partyMember.getPersonId());
			StaffVO enterpriseStaffVO = staffClient.detail(enterpriseStaff);
			Assert.notNull(enterpriseStaffVO,"找不到对应的员工信息");
			partyMember.setIdCard(partyMember.getIdCard());
			partyMember.setSex(enterpriseStaffVO.getSex());
			partyMember.setName(enterpriseStaffVO.getRealName());
			partyMember.setPhone(enterpriseStaffVO.getPhone());

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


}
