package cn.superdesk.uniorder.service.unified.impl;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProject;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProjectTarget;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedProjectMapper;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedProjectTargetMapper;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedProjectTargetVo;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedProjectVo;
import cn.superdesk.uniorder.service.unified.UnifiedProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UnifiedProjectServiceImpl implements UnifiedProjectService {


    @Autowired
    private UnifiedProjectMapper       unifiedProjectMapper;
    @Autowired
    private UnifiedProjectTargetMapper unifiedProjectTargetMapper;


    @Override
    public int insertSelective(UnifiedProject t) {

        return unifiedProjectMapper.insertSelective(t);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedProject t) {

        return unifiedProjectMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteObj(Long id) {

        unifiedProjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UnifiedProject findById(Long id) {

        return unifiedProjectMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<UnifiedProject> findAll(Map<String, Object> queryMap) {

        return null;
    }

    @Override
    public PageInfo<UnifiedProject> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

        return null;
    }

    @Override
    public UnifiedProjectTarget updateTarget(UnifiedProjectTarget target) {

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("projectId", target.getProjectId());
        queryMap.put("yearly", target.getYearly());
        queryMap.put("monthly", target.getMonthly());
        UnifiedProjectTarget search = unifiedProjectTargetMapper.selectByProjectIdAndMonth(queryMap);
        if (search != null) {
            target.setId(search.getId());
            unifiedProjectTargetMapper.updateByPrimaryKeySelective(target);

        } else {
            unifiedProjectTargetMapper.insert(target);
        }
        return target;
    }

    @Override
    public R<List<UnifiedProjectVo>> getDataByPage(Map<String, Object> params, int pageNo,
                                                   int pageSize) {

        PageHelper.startPage(pageNo, pageSize);

        List<UnifiedProjectVo> ls = unifiedProjectMapper.getData(params);

        PageInfo<UnifiedProjectVo> page = new PageInfo<UnifiedProjectVo>(ls);
        PageHelper.clearPage();
        return new R<List<UnifiedProjectVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);

    }

    @Override
    public R<List<UnifiedProjectTargetVo>> getTargetDataByPage(Map<String, Object> params, int pageNo,
                                                               int pageSize) {

        PageHelper.startPage(pageNo, pageSize);

        List<UnifiedProjectTargetVo> ls = unifiedProjectTargetMapper.getData(params);

        PageInfo<UnifiedProjectTargetVo> page = new PageInfo<UnifiedProjectTargetVo>(ls);
        PageHelper.clearPage();
        return new R<List<UnifiedProjectTargetVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);

    }

}
