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

import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.service.grid.IGridService;
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.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.systembase.dao.lrs.ILrsDao;
import cn.com.tenlion.systembase.pojo.dtos.lrs.LrsDTO;
import cn.com.tenlion.systembase.pojo.vos.lrs.LrsVO;
import cn.com.tenlion.systembase.pojo.bos.lrs.LrsBO;
import cn.com.tenlion.systembase.pojo.pos.lrs.LrsPO;
import cn.com.tenlion.systembase.service.lrs.ILrsService;
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.stereotype.Service;

import java.util.*;

/**
 * @ClassName: LrsServiceImpl
 * @Description: 综治领导责任制
 * @Author: CodeFactory
 * @Date: 2021-10-11 17:25:26
 * @Version: 3.0
 **/
@Service
public class LrsServiceImpl extends DefaultBaseService implements ILrsService {

    @Autowired
    private ILrsDao lrsDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(LrsVO lrsVO) {
        saveReturnId(lrsVO);
    }

    @Override
    public void save(String token, LrsVO lrsVO) {
        saveReturnId(token, lrsVO);
    }

    @Override
    public String saveReturnId(LrsVO lrsVO) {
        return saveReturnId(null, lrsVO);
    }

    @Override
    public String saveReturnId(String token, LrsVO lrsVO) {
        String lrsId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(lrsVO);
        params.put("lrsId", lrsId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        lrsDao.save(params);
        return lrsId;
    }

    @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("lrsIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        lrsDao.remove(params);
    }

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

    @Override
    public void update(String lrsId, LrsVO lrsVO) {
        update(null, lrsId, lrsVO);
    }

    @Override
    public void update(String token, String lrsId, LrsVO lrsVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(lrsVO);
        params.put("lrsId", lrsId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        lrsDao.update(params);
    }

    @Override
    public LrsDTO get(Map<String, Object> params) {
        LrsDTO lrsDTO = lrsDao.get(params);
        if(null != lrsDTO) {
            GridDTO gridDTO = gridService.get(lrsDTO.getGrid());
            if(null != gridDTO) {
                lrsDTO.setGridName(gridDTO.getGridName());
            }
        }
        return lrsDTO;
    }

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

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

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

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

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

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

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

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

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

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

}