package cn.com.countrygarden.bi.main.service.marketing.impl;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.marketing.CommOperationHistoryMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.marketing.CommOperationHistory;
import cn.com.countrygarden.bi.main.service.marketing.CommOperationHistoryService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import com.github.pagehelper.PageRowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
@Transactional
public class CommOperationHistoryServiceImpl extends AbstractService<CommOperationHistory> implements CommOperationHistoryService {
    @Resource
    private CommOperationHistoryMapper mapper;

    /**
     * 添加事件操作
     *
     * @param commId
     * @param name
     * @param memo
     * @param content
     * @param userCode
     */
    public void operation(int commId, String name, String memo, String content, String userCode) {
        CommOperationHistory operationHistory = new CommOperationHistory();
        operationHistory.setCommId(commId);
        operationHistory.setName(name);
        operationHistory.setComment(memo);
        operationHistory.setContent(content);
        operationHistory.setGmtCreate(new Date());
        operationHistory.setCreateBy(userCode);
        this.save(operationHistory);
    }

    /**
     * @param commId
     * @param page
     * @param pageSize
     * @return
     */
    public PageData search(int commId, int page, int pageSize) {
        List<Map<String, Object>> operations = new ArrayList<>();
        PageRowBounds pageRowBounds = new PageRowBounds(page, pageSize);
        List<CommOperationHistory> operationHistories = mapper.search(new HashMap<String, Object>() {{
            put("commId", commId);
        }}, pageRowBounds);

        for (CommOperationHistory operationHistory : operationHistories) {
            operations.add(new HashMap<String, Object>() {{
                put("id", operationHistory.getId());
                put("time", DateUtil.simpleFormat(operationHistory.getGmtCreate()));
                put("event", operationHistory.getName());
                put("remark", operationHistory.getComment());
            }});
        }

        PageData pageData = new PageData(operations, pageRowBounds);
        return pageData;
    }
}
