package jj.tech.paolu.biz.webadmin.controller;

import static org.mybatis.dynamic.sql.SqlBuilder.count;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualToWhenPresent;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

import java.util.function.Function;

import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.where.WhereApplier;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jj.tech.paolu.biz.webadmin.dao.SelectMapper;
import jj.tech.paolu.biz.webadmin.vo.Id;
import jj.tech.paolu.biz.webadmin.vo.Idc9998AdminAddVo;
import jj.tech.paolu.biz.webadmin.vo.Idc9998AdminListVo;
import jj.tech.paolu.biz.webadmin.vo.Idc9998AdminUpdateVo;
import jj.tech.paolu.repository.mybatis.dao.Idc9998AdminInfoMapper;
import jj.tech.paolu.repository.mybatis.dao.support.Idc9998AdminInfoDynamicSqlSupport;
import jj.tech.paolu.repository.mybatis.entity.Idc9998AdminInfo;
import jj.tech.paolu.utils.IDHelp;
import jj.tech.paolu.utils.Page;
import jj.tech.paolu.utils.R;

@RestController
@RequestMapping(value = "/op/idc9998/admin",name = "账号管理,代采账号列表")
@Tag(name  = "Idc9998ProxyAccountContronller", description = "账号管理,代采账号列表")
public class Idc9998ProxyAccountContronller {
	
	@Autowired SelectMapper selectMapper;
	@Autowired Idc9998AdminInfoMapper idc9998AdminInfoMapper;
	
	
	@Operation(summary = "用于采集的账号列表")
	@PostMapping("/list")
	public Object list(@Validated @RequestBody Idc9998AdminListVo parame) {
		
		Page p = new Page(parame.pageNum, parame.pageSize);
		
		Function<QueryExpressionDSL.FromGatherer<SelectModel>, QueryExpressionDSL<SelectModel>> function =
	    s -> {
	    	var from = s
					.from(Idc9998AdminInfoDynamicSqlSupport.idc9998AdminInfo)
					;
			return from;
	    };
	    
		var where = SqlBuilder.where();
		where.and(Idc9998AdminInfoDynamicSqlSupport.manager_name, isEqualToWhenPresent(parame.manager_name));
		where.and(Idc9998AdminInfoDynamicSqlSupport.member_name, isEqualToWhenPresent(parame.member_name));
		where.and(Idc9998AdminInfoDynamicSqlSupport.user_name, isEqualToWhenPresent(parame.user_name));
		where.and(Idc9998AdminInfoDynamicSqlSupport.user_password, isEqualToWhenPresent(parame.user_password));
		where.and(Idc9998AdminInfoDynamicSqlSupport.domain, isEqualToWhenPresent(parame.domain));
		
		WhereApplier applier = where.toWhereApplier();
		
	    SelectStatementProvider provider = function.apply(select(Idc9998AdminInfoMapper.selectList))
				.applyWhere(applier)
				.configureStatement(config -> config.setNonRenderingWhereClauseAllowed(true))
				.groupBy(Idc9998AdminInfoDynamicSqlSupport.id)
				.orderBy(Idc9998AdminInfoDynamicSqlSupport.id.descending())
				.limit(p.getPageSize())
				.offset(p.limitStart())
				.build()
				.render(RenderingStrategies.MYBATIS3);
	    
	    
		SelectStatementProvider count = select(count())
		.from(function.apply(select(Idc9998AdminInfoMapper.selectList))
		.applyWhere(applier)
		.groupBy(Idc9998AdminInfoDynamicSqlSupport.id)
		.orderBy(Idc9998AdminInfoDynamicSqlSupport.id.descending()),
		"t1")
		.configureStatement(config -> config.setNonRenderingWhereClauseAllowed(true))
		.build()
		.render(RenderingStrategies.MYBATIS3);



		var list = selectMapper.selectMany(provider);
		long total = selectMapper.count(count);
		p.setList(list);
		p.setTotal(total);
		return R.SUCCESS(p);

	}
	
	
	
	@Operation(summary = "新增代采账号")
	@PostMapping("/add")
	public Object add(@Validated @RequestBody Idc9998AdminAddVo parame) {
		
		Idc9998AdminInfo bean = new Idc9998AdminInfo();
		BeanUtils.copyProperties(parame, bean);
		bean.setId(IDHelp.getInstance().nextId());
		
		idc9998AdminInfoMapper.insertSelective(bean);
		
		return R.SUCCESS(bean);
	}
	
	@Operation(summary = "", description = "")
	@PostMapping("/update")
	public Object update(@Validated @RequestBody Idc9998AdminUpdateVo parame) {
		Idc9998AdminInfo bean = new Idc9998AdminInfo();
		BeanUtils.copyProperties(parame, bean);
		idc9998AdminInfoMapper.updateByPrimaryKeySelective(bean);
		return R.SUCCESS(bean);
	}
	
	@Operation(summary = "", description = "")
	@PostMapping("/get")
	public Object get(@Validated @RequestBody Id parame) {
		Idc9998AdminInfo bean =
		idc9998AdminInfoMapper.selectByPrimaryKey(parame.id).orElse(null);
		return R.SUCCESS(bean);
	}
	
	@Operation(summary = "", description = "")
	@PostMapping("/delete")
	public Object delete(@Validated @RequestBody Id parame) {
		idc9998AdminInfoMapper.deleteByPrimaryKey(parame.id);
		return R.SUCCESS("ok");
	}

}
