package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.FileUploadMapper;
import com.mvc.entity.FileUpload;
import com.mvc.entity.FileUploadExample;
import com.mvc.model.FileUploadModel;
import com.mvc.model.FileUploadTypeModel;
import com.mvc.netty.dealconstant.ResourcePath;
import com.mvc.netty.dealconstant.ResourceTag;
import com.mvc.utils.DateUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class FileUploadService {

    @Resource
    private FileUploadMapper fileUploadMapper;

    @Resource
    private Environment env;

    @Resource
    private FileUploadTypeService fileUploadTypeService;

    public Map<String, Object> findAllFileUpload(FileUploadModel model) {
        FileUploadExample example = new FileUploadExample();
        FileUploadExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(model.getFiletypecode())) {
            criteria.andFiletypecodeEqualTo(model.getFiletypecode());
        }
        if (StringUtils.isNotEmpty(model.getFilename())) {
            criteria.andFilenameLike(StringUtil.getLikeFormat(model.getFilename()));
        }
        if (StringUtils.isNotEmpty(model.getUploadman())) {
            criteria.andUploadmanLike(model.getUploadman());
        }
        example.setOrderByClause("uploadtime desc");
        Map<String, Object> resMap = new HashMap();
        int page = model.getPage();
        int rows = model.getRows();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<FileUpload> FileUploads = fileUploadMapper.selectByExample(example);
        List<FileUploadModel> resList = new ArrayList<>();
        for (FileUpload FileUpload : FileUploads) {
            FileUploadModel uploadmodel = new FileUploadModel();
            BeanUtils.copyProperties(FileUpload, uploadmodel);
            resList.add(uploadmodel);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public FileUploadModel saveFileUpload(FileUploadModel resModel) {
        FileUpload fileUpload = new FileUpload();
        BeanUtils.copyProperties(resModel,fileUpload);
        FileUploadTypeModel typeModel = fileUploadTypeService.findFileUploadTypeByCode(resModel.getFiletypecode());
        fileUpload.setFiletypename(typeModel == null ? "" : typeModel.getName());
        String time = DateUtil.getCurrentTime("yyyyMMddHHmmss");
        String path = env.getProperty(ResourceTag.ResourceUrlTag.getTag()) + File.separator + ResourcePath.download + File.separator + time + resModel.getFilename();
        fileUpload.setFilepath(path);
        fileUpload.setUploadtime(new Date());
        fileUploadMapper.insert(fileUpload);
        BeanUtils.copyProperties(fileUpload, resModel);
        resModel.setId(fileUpload.getId());
        return resModel;
    }

    public FileUploadModel updateFileUpload(FileUploadModel model) {
        FileUpload FileUpload = fileUploadMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, FileUpload);
        int i = fileUploadMapper.updateByPrimaryKey(FileUpload);
        return model;
    }

    public int deleteFileUpload(int id) {
        int i = fileUploadMapper.deleteByPrimaryKey(id);
        return i;
    }


}
