package com.bootdo.datas.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.bootdo.common.utils.StringUtils;
import com.bootdo.datas.domain.GyUnitDO;
import com.bootdo.datas.dto.GyDataImportDTO;
import com.bootdo.datas.service.GyDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.datas.dao.UnitDao;
import com.bootdo.datas.domain.UnitDO;
import com.bootdo.datas.service.UnitService;
import com.bootdo.system.domain.UserDO;

@Service
public class UnitServiceImpl implements UnitService {
    private static Logger log = LoggerFactory.getLogger(UnitServiceImpl.class);
    @Autowired
    private UnitDao unitDao;

    @Value("${managerValue}")
    public String managerValue;

    @Value("${sysProvince:#{null}}")
    public String sysProvince;

    @Value("${sysCity:#{null}}")
    public String sysCity;

    @Autowired
    private UnitService unitService;

    @Autowired
    private GyDataService gyDataService;

    @Override
    public GyDataImportDTO get(Long id) {
        return unitDao.get(id);
    }

    @Override
    public List<GyUnitDO> list(Map<String, Object> map) {
        UserDO currUser = ShiroUtils.getUser();
        log.info("currUser:" + currUser.toString());
        String province = currUser.getProvince();
        if(StringUtils.isEmpty(province)){
            province = sysProvince;
        }
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String gyUnitId = currUser.getGyUnitId();
        if (!managerValue.contains(currUser.getDeptId().toString())) {
            map.put("unitProvince", province);
            map.put("unitCity", city);
            map.put("unitId", gyUnitId);
        }
        return unitDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        UserDO currUser = ShiroUtils.getUser();
        String province = currUser.getProvince();
        if(StringUtils.isEmpty(province)){
            province = sysProvince;
        }
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String gyUnitId = currUser.getGyUnitId();
        if (!managerValue.contains(currUser.getDeptId().toString())) {
            map.put("unitProvince", province);
            map.put("unitCity", city);
            map.put("unitId", gyUnitId);
        }
        return unitDao.count(map);
    }

    @Override
    public int save(UnitDO unit) {
        return unitDao.save(unit);
    }


    @Override
    public int batchRemove(Long[] ids) {
        for (Long l:ids) {
            Map<String, Object> params = new HashMap<>();
            GyDataImportDTO unit = unitService.get(l);
            params.put("gyUnitId",unit.getCreditCode());
            int total = gyDataService.countTotal4Del(params);
            if (total > 0){
                return 0;
            }
        }
        return unitDao.batchRemove(ids);
    }

    @Override
    public GyUnitDO getUnitById(Long id) {
        return unitDao.getUnitById(id);
    }


}
