package ${basePackage}.service.${lowerTableName}.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import ${basePackage}.dao.${lowerTableName}.I${firstUpperTableName}Dao;
import ${basePackage}.pojo.dtos.${lowerTableName}.${firstUpperTableName}DTO;
import ${basePackage}.pojo.vos.${lowerTableName}.${firstUpperTableName}VO;
import ${basePackage}.service.${lowerTableName}.I${firstUpperTableName}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: ${firstUpperTableName}ServiceImpl
 * @Description: ${tableExplain}
 * @Author: ${author}
 * @Date: ${date}
 * @Version: ${version}
 **/
@Service
public class ${firstUpperTableName}ServiceImpl extends AbstractService implements I${firstUpperTableName}Service {

    @Autowired
    private I${firstUpperTableName}Dao ${firstLowerTableName}Dao;

    @Override
    public SuccessResult save${firstUpperTableName}(${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        save${firstUpperTableName}Info(null, ${firstLowerTableName}VO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult save${firstUpperTableName}ByToken(String token, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        save${firstUpperTableName}Info(token, ${firstLowerTableName}VO);
        return new SuccessResult();
    }

    @Override
    public String save${firstUpperTableName}ReturnId(${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        return save${firstUpperTableName}InfoReturnId(null, ${firstLowerTableName}VO);
    }

    @Override
    public String save${firstUpperTableName}ByTokenReturnId(String token, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        return save${firstUpperTableName}InfoReturnId(token, ${firstLowerTableName}VO);
    }

    /**
     * 新增${tableExplain}
     *
     * @param token
     * @param ${firstLowerTableName}VO
     * @throws Exception
     */
    private void save${firstUpperTableName}Info(String token, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        save${firstUpperTableName}InfoReturnId(token, ${firstLowerTableName}VO);
    }

    /**
     * 新增${tableExplain}
     *
     * @param token
     * @param ${firstLowerTableName}VO
     * @return ${firstLowerTableName}Id
     * @throws Exception
     */
    private String save${firstUpperTableName}InfoReturnId(String token, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        String ${firstLowerTableName}Id = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(${firstLowerTableName}VO);
        params.put("${firstLowerTableName}Id", ${firstLowerTableName}Id);
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        ${firstLowerTableName}Dao.save${firstUpperTableName}(params);
        return ${firstLowerTableName}Id;
    }

    @Override
    public SuccessResult remove${firstUpperTableName}(String ids) throws RemoveException {
        remove${firstUpperTableName}Info(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult remove${firstUpperTableName}ByToken(String token, String ids) throws RemoveException {
        remove${firstUpperTableName}Info(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除${tableExplain}
     *
     * @param token
     * @param ids
     */
    private void remove${firstUpperTableName}Info(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("${firstLowerTableName}Ids", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        ${firstLowerTableName}Dao.remove${firstUpperTableName}(params);
    }

    @Override
    public void delete${firstUpperTableName}(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("${firstLowerTableName}Ids", Arrays.asList(ids.split("_")));
        ${firstLowerTableName}Dao.delete${firstUpperTableName}(params);
    }

    @Override
    public SuccessResult update${firstUpperTableName}(String ${firstLowerTableName}Id, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        update${firstUpperTableName}Info(null, ${firstLowerTableName}Id, ${firstLowerTableName}VO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult update${firstUpperTableName}ByToken(String token, String ${firstLowerTableName}Id, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        update${firstUpperTableName}Info(token, ${firstLowerTableName}Id, ${firstLowerTableName}VO);
        return new SuccessResult();
    }

    /**
     * 修改${tableExplain}
     *
     * @param token
     * @param ${firstLowerTableName}Id
     * @param ${firstLowerTableName}VO
     */
    private void update${firstUpperTableName}Info(String token, String ${firstLowerTableName}Id, ${firstUpperTableName}VO ${firstLowerTableName}VO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(${firstLowerTableName}VO);
        params.put("${firstLowerTableName}Id", ${firstLowerTableName}Id);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        ${firstLowerTableName}Dao.update${firstUpperTableName}(params);
    }

    @Override
    public ${firstUpperTableName}DTO get${firstUpperTableName}ById(String ${firstLowerTableName}Id) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("${firstLowerTableName}Id", ${firstLowerTableName}Id);
        return ${firstLowerTableName}Dao.get${firstUpperTableName}(params);
    }

    @Override
    public List<${firstUpperTableName}DTO> list${firstUpperTableName}(Map<String, Object> params) throws SearchException {
        return ${firstLowerTableName}Dao.list${firstUpperTableName}(params);
    }

    @Override
    public SuccessResultList<List<${firstUpperTableName}DTO>> listPage${firstUpperTableName}(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<${firstUpperTableName}DTO> ${firstLowerTableName}DTOs = ${firstLowerTableName}Dao.list${firstUpperTableName}(page.getParams());
        PageInfo<${firstUpperTableName}DTO> pageInfo = new PageInfo<>(${firstLowerTableName}DTOs);
        return new SuccessResultList<>(${firstLowerTableName}DTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer countNumber${firstUpperTableName}(Map<String, Object> params) throws SearchException {
        Integer count = ${firstLowerTableName}Dao.count${firstUpperTableName}(params);
        return count == null ? 0 : count;
    }

    @Override
    public SuccessResultData<Integer> count${firstUpperTableName}(Map<String, Object> params) throws SearchException {
        return new SuccessResultData<>(countNumber${firstUpperTableName}(params));
    }

}