package org.springblade.community.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.expand.feign.IConfClient;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.util.QueryWrapperUtil;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.community.entity.Agency;
import org.springblade.community.vo.AgencyVO;
import org.springblade.community.wrapper.AgencyWrapper;
import org.springblade.community.service.IAgencyService;

import java.util.List;
import java.util.Map;

/**
 * client
 *
 * @author lianghanqiang
 * @since 2021-03-29
 */

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

public class AgencyClient implements IAgencyClient {

	private final IAgencyService agencyService;
	private final IOrgPositionClient orgPositionClient;
	private final IConfClient confClient;

	/**
	 * 详情
	 */
	@Override
	@GetMapping("/detail")
	public AgencyVO detail(@SpringQueryMap Agency agency) {
		Agency detail = agencyService.getOne(Condition.getQueryWrapper(agency));
		return AgencyWrapper.build(confClient).entityVO(detail);
	}

	@Override
	@GetMapping("/getById")
	public Agency getById(@RequestParam Long id) {
		return this.agencyService.getById(id);
	}

	/**
	 * 查询
	 */
	@Override
	@GetMapping("/list")
	public List<AgencyVO> list(@SpringQueryMap Agency agency) {
		LambdaQueryWrapper<Agency> queryWrapper = new QueryWrapperUtil<Agency>().getBasicQueryWrapper(Agency::getOrgPosition, agency, AuthUtil.getUser(), orgPositionClient);
		return AgencyWrapper.build(confClient).listVO(agencyService.list(queryWrapper));
	}

	@Override
	public List<AgencyVO> listWithOutToken(@SpringQueryMap Agency agency) {
		return AgencyWrapper.build(confClient).listVO(agencyService.list(Condition.getQueryWrapper(agency)));
	}


	/**
	 * 新增
	 */
	@Override
	@PostMapping("/save")
	public boolean save(@Valid @RequestBody Agency agency) {
		return agencyService.save(agency);
	}

	/**
	 * 修改
	 */
	@Override
	@PostMapping("/update")
	public boolean update(@Valid @RequestBody Agency agency) {
		return agencyService.updateById(agency);
	}

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


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

	/**
	 * 分页
	 */
	@PostMapping("/wrapperlist")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入agency")
	public List<Agency> wrapperlist(@RequestBody LambdaQueryWrapper<Agency> wrapper) {
		return agencyService.getBaseMapper().selectList(wrapper);
	}

	@Override
	@PostMapping("/listForOrg")
	public List<Agency> listForOrg(@RequestBody Object[] params) {
		LambdaQueryWrapper<Agency> enterpriseLambdaQueryWrapper =
			new QueryWrapperUtil<Agency>()
				.getBasicQueryWrapper(
					Agency::getOrgPosition,
					BeanUtil.toBean((Map<String, Object>) params[0], Agency.class),
					BeanUtil.toBean((Map<String, Object>) params[1], BladeUser.class),
					orgPositionClient
				);
		return agencyService.getBaseMapper().selectList(enterpriseLambdaQueryWrapper);
	}
}
