package cn.com.tenlion.systembase.service.enterprisepatrol.impl;

import cn.com.tenlion.systembase.dao.enterprisepatrol.IEnterprisePatrolDao;
import cn.com.tenlion.systembase.pojo.bos.enterprisepatrol.EnterprisePatrolBO;
import cn.com.tenlion.systembase.pojo.dtos.enterprisepatrol.EnterprisePatrolDTO;
import cn.com.tenlion.systembase.pojo.pos.enterprisepatrol.EnterprisePatrolPO;
import cn.com.tenlion.systembase.pojo.vos.enterprisepatrol.EnterprisePatrolVO;
import cn.com.tenlion.systembase.service.enterprisepatrol.IEnterprisePatrolService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.date.DateUtil;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: EnterprisePatrolServiceImpl
 * @Description: 企业巡查记录
 * @Author: CodeFactory
 * @Date: 2022-01-07 15:23:28
 * @Version: 3.0
 **/
@Service
public class EnterprisePatrolServiceImpl extends DefaultBaseService implements IEnterprisePatrolService {

    @Autowired
    private IEnterprisePatrolDao enterprisePatrolDao;

    @Override
    public void save(EnterprisePatrolVO enterprisePatrolVO) {
        saveReturnId(enterprisePatrolVO);
    }

    @Override
    public void save(String token, EnterprisePatrolVO enterprisePatrolVO) {
        saveReturnId(token, enterprisePatrolVO);
    }

    @Override
    public String saveReturnId(EnterprisePatrolVO enterprisePatrolVO) {
        return saveReturnId(null, enterprisePatrolVO);
    }

    @Override
    public String saveReturnId(String token, EnterprisePatrolVO enterprisePatrolVO) {
        String enterprisePatrolId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(enterprisePatrolVO);
        params.put("enterprisePatrolId", enterprisePatrolId);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        try {
            if (null != authentication) {
                if (StringUtils.isBlank(token)) {
                    setSaveInfo(params);
                } else {
                    setAppSaveInfo(token, params);
                }
            } else {
                String currentDate = DateUtil.getTime();
                params.put("creator", 1);
                params.put("gmtCreate", currentDate);
                params.put("modifier", 1);
                params.put("gmtModified", currentDate);
                params.put("isDelete", 0);
            }
        }catch (Exception e) {

        }
        if(null == params.get("creator")) {
            String currentDate = DateUtil.getTime();
            params.put("creator", 1);
            params.put("gmtCreate", currentDate);
            params.put("modifier", 1);
            params.put("gmtModified", currentDate);
            params.put("isDelete", 0);
        }
        enterprisePatrolDao.save(params);
        return enterprisePatrolId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("enterprisePatrolIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        enterprisePatrolDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("enterprisePatrolIds", ids);
        enterprisePatrolDao.delete(params);
    }

    @Override
    public void update(String enterprisePatrolId, EnterprisePatrolVO enterprisePatrolVO) {
        update(null, enterprisePatrolId, enterprisePatrolVO);
    }

    @Override
    public void update(String token, String enterprisePatrolId, EnterprisePatrolVO enterprisePatrolVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(enterprisePatrolVO);
        params.put("enterprisePatrolId", enterprisePatrolId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        enterprisePatrolDao.update(params);
    }

    @Override
    public EnterprisePatrolDTO get(Map<String, Object> params) {
        return enterprisePatrolDao.get(params);
    }

    @Override
    public EnterprisePatrolDTO get(String enterprisePatrolId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("enterprisePatrolId", enterprisePatrolId);
        return get(params);
    }

    @Override
    public EnterprisePatrolBO getBO(Map<String, Object> params) {
        return enterprisePatrolDao.getBO(params);
    }

    @Override
    public EnterprisePatrolBO getBO(String enterprisePatrolId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("enterprisePatrolId", enterprisePatrolId);
        return getBO(params);
    }

    @Override
    public EnterprisePatrolPO getPO(Map<String, Object> params) {
        return enterprisePatrolDao.getPO(params);
    }

    @Override
    public EnterprisePatrolPO getPO(String enterprisePatrolId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("enterprisePatrolId", enterprisePatrolId);
        return getPO(params);
    }

    @Override
    public List<EnterprisePatrolDTO> list(Map<String, Object> params) {
        return enterprisePatrolDao.list(params);
    }

    @Override
    public List<EnterprisePatrolBO> listBO(Map<String, Object> params) {
        return enterprisePatrolDao.listBO(params);
    }

    @Override
    public List<EnterprisePatrolPO> listPO(Map<String, Object> params) {
        return enterprisePatrolDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<EnterprisePatrolDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<EnterprisePatrolDTO> enterprisePatrolDTOs = list(page.getParams());
        PageInfo<EnterprisePatrolDTO> pageInfo = new PageInfo<>(enterprisePatrolDTOs);
        return new SuccessResultList<>(enterprisePatrolDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = enterprisePatrolDao.count(params);
        return count == null ? 0 : count;
    }

}