package com.bonus.hn.bearing.capacity.backstage.manager.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.TreeBean;
import com.bonus.hn.bearing.capacity.backstage.manager.dao.AnnouncementDao;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.AnnouncementBean;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.DownloadFileBean;
import com.bonus.hn.bearing.capacity.backstage.manager.service.AnnouncementService;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.project.entity.FileBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author bonus
 */
@Service(value = "AnnouncementService")
public class AnnouncementServiceImpl implements AnnouncementService {

    @Resource(name = "AnnouncementDao")
    private AnnouncementDao dao;

    @Override
    public List<AnnouncementBean> getAnnouncementList(AnnouncementBean o) {
        return dao.getAnnouncementList(o);
    }

    @Override
    @Transactional
    public int addAnnouncementInfo(AnnouncementBean bean) {
        bean.setReleasePerson(UserUtil.getLoginUser().getUsername());
        bean.setReleasePersonId(UserUtil.getLoginUser().getId() + "");
        int num = dao.addAnnouncementInfo(bean);
        if (num == 1) {
            if ("1".equals(bean.getPersonType())) {
                List<String> ids = dao.getAllPerson();
                for (int i = 0; i < ids.size(); i++) {
                    num = dao.insertUserNoticeRelation(bean.getId(), ids.get(i));
                }
            } else {
                String userId = bean.getUserId();
                String[] userIdArr = userId.split(",");
                for (int i = 0; i < userIdArr.length; i++) {
                    num = dao.insertUserNoticeRelation(bean.getId(), userIdArr[i]);
                }
            }
        }
        return num;
    }

    @Override
    public int checkTitle(AnnouncementBean o) {
        return dao.checkTitle(o);
    }

    @Override
    public List<AnnouncementBean> getAnnouncementType(AnnouncementBean o) {
        return dao.getAnnouncementType(o);
    }

    @Override
    public int delAnnouncementTypeById(AnnouncementBean bean) {
        return dao.delAnnouncementTypeById(bean);
    }

    @Override
    public int delAnnouncementById(AnnouncementBean bean) {
        return dao.delAnnouncementById(bean);
    }

    @Override
    public AnnouncementBean getAnnouncementView(AnnouncementBean o) {
        return dao.getAnnouncementView(o);
    }

    @Override
    public List<FileBean> getFileList(FileBean o) {
        return dao.getFileList(o);
    }

    @Override
    public List<AnnouncementBean> getUserList(AnnouncementBean o) {
        return dao.getUserList(o);
    }

    @Override
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        return dao.getCompanyProTree(o);
    }

    @Override
    public List<TreeBean> personTree(TreeBean o) {
        return dao.getPersonTree(o);
    }

    @Override
    @Transactional
    public int categoricalManagementOfOperations(AnnouncementBean bean) {
        int num = 0;
        String finalData = bean.getFinalData();
        JSONArray dArray = JSONObject.parseArray(finalData);
        for (int i = 0; i < dArray.size(); i++) {
            JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
            AnnouncementBean b = new AnnouncementBean();
            b.setTypeName(dObject.get("typeName").toString());
            b.setParentId(dObject.get("parentId").toString());
            b.setId(dObject.get("id").toString());
            if ("1".equals(dObject.get("type").toString()) || "2".equals(dObject.get("type").toString())) {
                //修改
                num = dao.updateCateManage(b);
            } else {
                //新增
                num = dao.insertCateManage(b);
            }
        }
        return num;
    }

    @Override
    public int insertFileDownLoadRecord(DownloadFileBean bean) {
        bean.setUserName(UserUtil.getLoginUser().getUsername());
        bean.setUserId(UserUtil.getLoginUser().getId()+"");
        return dao.insertFileDownLoadRecord(bean);
    }
}
