package org.springblade.enterprise.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import net.sf.cglib.beans.BeanMap;
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.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.enterprise.entity.Enterprise;
import org.springblade.enterprise.vo.EnterpriseVO;
import org.springblade.enterprise.wrapper.EnterpriseWrapper;
import org.springblade.enterprise.service.IEnterpriseService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
import java.util.Map;

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

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

public class EnterpriseClient implements IEnterpriseClient {
	private IOrgPositionClient orgPositionClient;
	private final IEnterpriseService enterpriseService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public EnterpriseVO detail(@SpringQueryMap Enterprise enterprise) {
        Enterprise detail = enterpriseService.getOne(Condition.getQueryWrapper(enterprise));
        return EnterpriseWrapper.build().entityVO(detail);
        }

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

	/**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<EnterpriseVO> list(@SpringQueryMap Enterprise enterprise) {
        return EnterpriseWrapper.build().listVO(enterpriseService.list(Condition.getQueryWrapper(enterprise).lambda().eq(Enterprise::getTenantId, AuthUtil.getUser().getTenantId())));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Enterprise enterprise) {
        return enterpriseService.save(enterprise);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Enterprise enterprise) {
        return enterpriseService.updateById(enterprise);
        }

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


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

	@Override
	public List<Enterprise> listForOrg(List<Object> params) {
		LambdaQueryWrapper<Enterprise> enterpriseLambdaQueryWrapper =
			new QueryWrapperUtil<Enterprise>()
				.getBasicQueryWrapper(
					Enterprise::getOrgPosition,
					BeanUtil.toBean((Map<String, Object>) params.get(0),Enterprise.class),
					BeanUtil.toBean((Map<String, Object>) params.get(1),BladeUser.class),
					orgPositionClient
				);
		return enterpriseService.getBaseMapper().selectList(enterpriseLambdaQueryWrapper);
	}

	@Override
	@GetMapping("/count")
	public Integer count(@RequestParam(value = "tenantId", required = false) String tenantId) {
    	return enterpriseService.count();
	}


}
