package com.admin.major.service.impl;

import java.util.List;
import com.admin.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import com.admin.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.admin.major.domain.ProposalFile;
import com.admin.major.mapper.ProposalMapper;
import com.admin.major.domain.Proposal;
import com.admin.major.service.IProposalService;

/**
 * 用户建议记录Service业务层处理
 * 
 * @author zewei
 * @date 2022-05-31
 */
@Service
public class ProposalServiceImpl implements IProposalService 
{
    @Autowired
    private ProposalMapper proposalMapper;

    /**
     * 查询用户建议记录
     * 
     * @param id 用户建议记录主键
     * @return 用户建议记录
     */
    @Override
    public Proposal selectProposalById(Long id)
    {
        return proposalMapper.selectProposalById(id);
    }

    /**
     * 查询用户建议记录列表
     * 
     * @param proposal 用户建议记录
     * @return 用户建议记录
     */
    @Override
    public List<Proposal> selectProposalList(Proposal proposal)
    {
        return proposalMapper.selectProposalList(proposal);
    }



    /**
     * 查询用户建议记录
     *
     * @param proposal 用户建议记录
     * @return 用户建议记录
     */
    @Override
    public Proposal selectOneProposal(Proposal proposal)
    {
        return proposalMapper.selectProposalList(proposal)
                .stream()
                .findFirst()
                .orElse(null);
    }

    /**
     * 新增用户建议记录
     *
     * @param proposal 用户建议记录
     * @return 结果
     */

    @Transactional
    @Override
    public int insertProposal(Proposal proposal)
    {
        proposal.setCreateTime(DateUtils.getNowDate());
        int rows = proposalMapper.insertProposal(proposal);
        insertProposalFile(proposal);
        return rows;
    }

    /**
     * 修改用户建议记录
     * 
     * @param proposal 用户建议记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProposal(Proposal proposal)
    {
        proposalMapper.deleteProposalFileByProposalId(proposal.getId());
        insertProposalFile(proposal);
        return proposalMapper.updateProposal(proposal);
    }

    /**
     * 批量删除用户建议记录
     * 
     * @param ids 需要删除的用户建议记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProposalByIds(Long[] ids)
    {
        proposalMapper.deleteProposalFileByProposalIds(ids);
        return proposalMapper.deleteProposalByIds(ids);
    }

    /**
     * 删除用户建议记录信息
     * 
     * @param id 用户建议记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProposalById(Long id)
    {
        proposalMapper.deleteProposalFileByProposalId(id);
        return proposalMapper.deleteProposalById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param proposal 用户建议记录对象
     */
    public void insertProposalFile(Proposal proposal)
    {
        List<ProposalFile> proposalFileList = proposal.getProposalFileList();
        Long id = proposal.getId();
        if (StringUtils.isNotNull(proposalFileList))
        {
            List<ProposalFile> list = new ArrayList<ProposalFile>();
            for (ProposalFile proposalFile : proposalFileList)
            {
                proposalFile.setProposalId(id);
                list.add(proposalFile);
            }
            if (list.size() > 0)
            {
                proposalMapper.batchProposalFile(list);
            }
        }
    }
}
