package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dao.SsmCashDao;
import com.ssm.manage.dao.SsmProjectProgressDao;
import com.ssm.manage.entity.CashList;
import com.ssm.manage.entity.ProjectProgress;
import com.ssm.manage.entity.ProjectProgressFile;
import com.ssm.manage.query.ProjectProgressRequest;
import com.ssm.manage.service.SsmCashService;
import com.ssm.manage.service.SsmProjectProgressFileService;
import com.ssm.manage.service.SsmProjectProgressService;
import com.ssm.manage.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
public class SsmProjectProgressServiceImpl extends BaseServiceImpl<ProjectProgress> implements SsmProjectProgressService {
    @Autowired
    private SsmProjectProgressFileService ssmProjectProgressFileService;

    @Autowired
    private SsmCashDao ssmCashDao;

    @Autowired
    private SsmProjectProgressDao dao;

    @Override
    public BaseDao<ProjectProgress> getDao() {
        return dao;
    }

    @Override
    public PageInfo<ProjectProgress> selectPage(ProjectProgressRequest progressRequest) {
        return dao.selectPage(progressRequest);
    }

    @Override
    public ProjectProgress selectOne(ProjectProgressRequest progressRequest) {
        ProjectProgress projectProgress = new ProjectProgress();
        BeanUtils.copyProperties(progressRequest, projectProgress);
        List<ProjectProgress> list = dao.select(projectProgress);
        return list.get(0);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(ProjectProgressRequest progressRequest) {
        int i = 0;
        ProjectProgress projectProgress = new ProjectProgress();
        BeanUtils.copyProperties(progressRequest, projectProgress);
        Long id = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        projectProgress.setId(id);
        i = dao.saveNotNull(projectProgress);
        if (i > 0) {
            //将文件上传信息保存到数据库
            List<ProjectProgressFile> list = progressRequest.getUploadFiles();
            for (ProjectProgressFile uploadFiles : list) {
                uploadFiles.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                uploadFiles.setProgressId(id);
                ssmProjectProgressFileService.saveNotNull(uploadFiles);
            }
            //将对应收银单会员或者员工反馈状态做修改
            CashList cashList = new CashList();
            cashList.setId(projectProgress.getDocId());
            cashList.setCashDate(projectProgress.getDocDate());
            if (0 == progressRequest.getSource()) {
                cashList.setIsMemberFeedback(1);
            }else {
                cashList.setIsStaffFeedback(1);
            }
            ssmCashDao.updateFeedback(cashList);
        }
        return i;
    }

    @Override
    public int updateProjectProgress(ProjectProgressRequest progressRequest) {
        int i = 0;
        ProjectProgress projectProgress = new ProjectProgress();
        BeanUtils.copyProperties(progressRequest, projectProgress);
        i = dao.updateByPrimaryKeySelective(projectProgress);

        //将文件上传信息保存到数据库
        List<ProjectProgressFile> list = progressRequest.getUploadFiles();
        for (ProjectProgressFile uploadFiles : list) {
            uploadFiles.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            uploadFiles.setProgressId(projectProgress.getId());
            ssmProjectProgressFileService.saveNotNull(uploadFiles);
        }

        return i;
    }
}
