package org.springblade.system.expand.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.ConfConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.system.expand.entity.Conf;
import org.springblade.system.expand.vo.ConfVO;
import org.springblade.system.expand.wrapper.ConfWrapper;
import org.springblade.system.expand.service.IConfService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 * 系统配置表 client
 *
 * @author lianghanqiang
 * @since 2021-04-25
 */

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

public class ConfClient implements IConfClient {

    private final IConfService confService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public ConfVO detail(@SpringQueryMap Conf conf) {
        Conf detail = confService.getOne(Condition.getQueryWrapper(conf));
        return ConfWrapper.build().entityVO(detail);
        }

	@Override
	@GetMapping("/getValueByToken")
	public String getValueByToken(@RequestParam String token) {
		Assert.notNull(token,"token不能为空");
		Conf conf = confService.getOne(new QueryWrapper<>(new Conf()).lambda().eq(Conf::getConfToken, token));
		return conf != null ? conf.getConfValue() : "";
	}

	/**
     * 查询 系统配置表
     */
    @Override
    @GetMapping("/list")
    public List<ConfVO> list(@SpringQueryMap Conf conf) {
        return ConfWrapper.build().listVO(confService.list(Condition.getQueryWrapper(conf)));
        }



    /**
     * 新增 系统配置表
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Conf conf) {
        return confService.save(conf);
        }

    /**
     * 修改 系统配置表
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Conf conf) {
        return confService.updateById(conf);
        }

    /**
     * 新增或修改 系统配置表
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody Conf conf) {
        return confService.saveOrUpdate(conf);
        }


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

	@Override
	@GetMapping("/addPrefix")
	public String addPrefix(@RequestParam(required = false) String oldPicPath) {
    	if(oldPicPath == null || oldPicPath == ""){
    		return "";
		}
		return this.getPicPrefix(ConfConstant.FILE_SERVER_HOST)+oldPicPath;
	}

	@Override
	@GetMapping("/removePrefix")
	public String removePrefix(@RequestParam(required = false) String oldPicPath) {
		if(oldPicPath == null || oldPicPath == ""){
			return "";
		}
		return oldPicPath.replace(this.getPicPrefix(ConfConstant.MINIO_SERVER_HOST),"");
	}

	private String getPicPrefix(String confToken){
		Conf conf = confService.getBaseMapper().selectOne(new QueryWrapper<>(new Conf()).lambda().eq(Conf::getConfToken, confToken));
		if(conf != null){
			return conf.getConfValue();
		}else {
			return "";
		}
	}


}
