package ${basePackage}.module.${modulePackage}.service;

import com.baomidou.mybatisplus.plugins.Page;
import ${basePackage}.common.domain.PageResultDTO;
import ${basePackage}.common.domain.ResponseDTO;
import ${basePackage}.module.${modulePackage}.dao.${moduleClass}Dao;
import ${basePackage}.module.${modulePackage}.domain.dto.${moduleClass}DTO;
import ${basePackage}.module.${modulePackage}.domain.dto.${moduleClass}QueryDTO;
import ${basePackage}.module.${modulePackage}.domain.entity.${moduleClass}Entity;
import ${basePackage}.util.SmartPaginationUtil;
import ${basePackage}.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ ${tableDesc} ]
 *
 * @author ${author}
 * @version 1.0
 * @company ${company}
 * @copyright (c) 2019 ${company}Inc. All rights reserved.
 * @date ${date}
 * @since JDK1.8
 */
@Service
public class ${moduleClass}Service {

    @Autowired
    private ${moduleClass}Dao ${moduleVar}Dao;


    /**
     * @author ${author}
     * @description 分页查询
     * @date ${date}
     */
    public ResponseDTO<PageResultDTO<${moduleClass}DTO>> queryByPage(${moduleClass}QueryDTO queryDTO) {
        Page page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<${moduleClass}Entity> entities = ${moduleVar}Dao.queryByPage(page, queryDTO);
        List<${moduleClass}DTO> dtoList = SmartBeanUtil.copyList(entities, ${moduleClass}DTO.class);
        page.setRecords(dtoList);
        PageResultDTO<${moduleClass}DTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author ${author}
     * @description 添加
     * @date ${date}
     */
    public ResponseDTO<String> add(${moduleClass}DTO addDTO) {
        ${moduleClass}Entity entity = SmartBeanUtil.copy(addDTO, ${moduleClass}Entity.class);
        ${moduleVar}Dao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author ${author}
     * @description 编辑
     * @date ${date}
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(${moduleClass}DTO updateDTO) {
        ${moduleClass}Entity entity = SmartBeanUtil.copy(updateDTO, ${moduleClass}Entity.class);
        ${moduleVar}Dao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author ${author}
     * @description 删除
     * @date ${date}
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        ${moduleVar}Dao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author ${author}
     * @description  根据ID查询
     * @date ${date}
    */
    public ResponseDTO<${moduleClass}DTO> detail(Long id){
        ${moduleClass}Entity entity = ${moduleVar}Dao.selectById(id);
        ${moduleClass}DTO dto =  SmartBeanUtil.copy(entity,${moduleClass}DTO.class);
        return ResponseDTO.succData(dto);
    }
}
