package {{packageName}}.core.{{entity.module}}.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
{{#if entity.clientEntityModel}}
import {{packageName}}.core.{{entity.module}}.client.{{entity.clientEntityModel.codeName}}FeignClient;
{{/if}}
import org.springframework.beans.BeanUtils;
import cn.ibizlab.util.security.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.Assert;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
{{#system.enableGlobalTransaction}}
import io.seata.spring.annotation.GlobalTransactional;
{{/system.enableGlobalTransaction}}
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}};
import {{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext;
import {{packageName}}.core.{{entity.module}}.service.{{entity.codeName}}Service;
import cn.ibizlab.util.helper.CachedBeanCopier;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;


/**
 * 实体[{{entity.logicName}}] 服务对象接口实现
 */
@Slf4j
@Service("{{entity.codeName}}Service")
public class {{entity.codeName}}ServiceBase implements {{entity.codeName}}Service {

    {{#if entity.clientEntityModel}}
    @Autowired
    {{entity.clientEntityModel.codeName}}FeignClient {{camelCase entity.clientEntityModel.codeName}}FeignClient;
    {{/if}}

    protected int batchSize = 500;

    public {{entity.codeName}} get({{entity.codeName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.getBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}());
    }
    
    public List<{{entity.codeName}}> getByEntities(List<{{entity.codeName}}> entities) {
        return null;
    }

    public {{entity.codeName}} getDraft({{entity.codeName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.getDraft(et);
    }

    public Integer checkKey({{entity.codeName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.checkKey(et);
    }

    @Override
    {{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean create({{entity.codeName}} et) {
        {{entity.codeName}} rt = {{camelCase entity.clientEntityModel.codeName}}FeignClient.create(et);
        BeanUtils.copyProperties(rt, et);
        return true;
    }
    {{#eq entity.actions.create.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.create.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean createBatch(List<{{entity.codeName}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.createBatch(list);
    }

    {{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean update({{entity.codeName}} et) {
        {{entity.codeName}} rt = {{camelCase entity.clientEntityModel.codeName}}FeignClient.updateBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}(), et);
        BeanUtils.copyProperties(rt, et);
        return true;
    }

    {{#eq entity.actions.update.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.update.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean updateBatch(List<{{entity.codeName}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.updateBatch(list);
    }

    {{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean save({{entity.codeName}} et) {
        {{entity.codeName}} rt =  {{camelCase entity.clientEntityModel.codeName}}FeignClient.save(et);
        BeanUtils.copyProperties(rt, et);
        return true;
    }

    {{#eq entity.actions.save.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.save.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean saveBatch(List<{{entity.codeName}}> list) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.saveBatch(list);
    }

    {{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean remove({{entity.codeName}} et) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.removeBy{{pascalCase entity.keyField.codeName}}(et.get{{pascalCase entity.keyField.codeName}}());
    }
    
    {{#eq entity.actions.remove.transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq entity.actions.remove.transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public boolean removeByEntities(List<{{entity.codeName}}> entities) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.removeBatch(entities.stream().map(e->e.get{{pascalCase entity.keyField.codeName}}()).collect(Collectors.toList()));
    }

    {{#entity.extActions}}
    {{#eq transactionMode "GLOBAL"}}@GlobalTransactional{{/eq}}{{#eq transactionMode "DEFAULT"}}@Transactional{{/eq}}
    public {{outParam}} {{camelCase codeName}}({{inParam}} {{inParamName}}) {
        {{#if voidReturn}}
        return {{inParamName}};
        {{else}}
        return null;
        {{/if}}
    }

    {{/entity.extActions}}
    {{#entity.dataSets}}
    public Page<{{entity.codeName}}> search{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
        return {{camelCase entity.clientEntityModel.codeName}}FeignClient.fetch{{pascalCase codeName}}(context);
    }
    public List<{{entity.codeName}}> list{{pascalCase codeName}}({{entity.codeName}}SearchContext context) {
        return null;
    }

    {{/entity.dataSets}}
    {{#entity.references}}
    public List<{{entity.codeName}}> selectBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
        return null;
    }

    public boolean removeBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
        return false;
    }

    public boolean resetBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}}) {
        return false;
    }

    public boolean saveBy{{pascalCase fkField.codeName}}({{fkField.type.java}} {{camelCase fkField.codeName}},List<{{entity.codeName}}> list) {

        return false;
    }

    {{/entity.references}}
    {{#each entity.nesteds}}
        {{#if listCode}}
        {{#unless columnName}}
    public List<{{entityCodeName}}> get{{pascalCase codeName}}({{entity.codeName}} et) {
        return null;
    }

        {{/unless}}
        {{/if}}

    {{/each}}

}