package com.goafanti.standard.service.impl;

import com.goafanti.common.dao.StandardDocumentMapper;
import com.goafanti.common.model.StandardDocument;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.standard.bo.InputStandardList;
import com.goafanti.standard.bo.OutStandardDocument;
import com.goafanti.standard.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class StandardServiceImpl extends BaseMybatisDao<StandardDocumentMapper> implements StandardService {
    @Autowired
    private StandardDocumentMapper standardDocumentMapper;
    @Value("${upload.path}")
    private String uploadPath;

    @Override
    public Object add(StandardDocument in) {
        return standardDocumentMapper.insertSelective(in);
    }

    @Override
    public Object delete(StandardDocument in) {
        Long id = Long.valueOf(in.getId());
        StandardDocument use = standardDocumentMapper.selectByPrimaryKey(id);
        if (StringUtils.isNotEmpty(in.getUrl())){
            deleteFile(use.getUrl());
        }
        return standardDocumentMapper.deleteByPrimaryKey(id);
    }

    private void deleteFile( String url) {
        if (url.contains(",")){
            String[] split = url.split(",");
            for (String s : split) {
                String filePath = uploadPath + "/" + s;
                com.goafanti.common.utils.excel.FileUtils.deleteFile(filePath);
            }
        }else {
            String filePath = uploadPath + "/" + url;
            com.goafanti.common.utils.excel.FileUtils.deleteFile(filePath);
        }

    }

    private void deleteFile(String url, String newURL) {
        if(StringUtils.isBlank(newURL)){
         deleteFile(url);
        }else {
            String[] newU = newURL.split(",");
            String[] useU = url.split(",");
            for (String s : useU) {
                boolean flag = false;
                for (String s1 : newU) {
                    if (s.equals(s1)){
                        flag = true;
                        break;
                    }
                }
                if (!flag){
                    deleteFile(s);
                }
            }
        }

    }

    @Override
    public Object update(StandardDocument in) {
        Long id = Long.valueOf(in.getId());
        in.setUpdateTime(new Date());
        StandardDocument use = standardDocumentMapper.selectByPrimaryKey(id);
        if (StringUtils.isNotEmpty(use.getUrl())&&
                use.getUrl().equals(in.getUrl())){
            deleteFile(use.getUrl(),in.getUrl());
        }
        return standardDocumentMapper.updateByPrimaryKeySelective(in);
    }



    @Override
    public Pagination<OutStandardDocument> list(InputStandardList in) {
        Map<String,Object> params = new HashMap();
        if (in.getStatus()!=null)params.put("status",in.getStatus());
        if (in.getStartTime()!=null)params.put("startTime",in.getStartTime());
        if (in.getEndTime()!=null)params.put("endTime",in.getEndTime()+" 23:59:59");
        if (in.getDepId()!=null)params.put("depId",in.getDepId());
        if (in.getName()!=null)params.put("name",in.getName());
        if (in.getType()!=null)params.put("type",in.getType());
        return (Pagination<OutStandardDocument>) findPage("standardList","standardCount",params,in.getPageNo(),in.getPageSize());
    }

    @Override
    public Object get(StandardDocument in) {
        return standardDocumentMapper.selectDetailsById(in.getId());

    }

    @Override
    public boolean inspectNumber(StandardDocument in) {
        StandardDocument out = standardDocumentMapper.selectByNumber(in);
        if (in.getId()==null&&out!=null) return true;
        else return in.getId() != null && out != null && !Objects.equals(out.getId(), in.getId());
    }
}
