package cn.com.countrygarden.bi.main.service.districtReport.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.districtReport.RdDistrictProjectMpMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.districtReport.RdDistrictProjectMp;
import cn.com.countrygarden.bi.main.model.dup.DupComm;
import cn.com.countrygarden.bi.main.service.districtReport.RdDistrictProjectMpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;

/**
 * 片区erp项目对应-业务逻辑层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2019-08-14
 */
@Service
@Transactional
public class RdDistrictProjectMpServiceImpl implements RdDistrictProjectMpService{
    @Autowired
    private RdDistrictProjectMpMapper rdDistrictProjectMpMapper;

    @Autowired
    private ManagerMapForErpMapper managerMapForErpMapper;

    @Override
    public Result queryPageList(RdDistrictProjectMp rdDistrictProjectMp, int page, int size) {
        Page<Object> pageList = PageHelper.startPage(page, size);
        Condition condition=new Condition(RdDistrictProjectMp.class);
        Criteria criteria = condition.createCriteria();
//		TODO 添加条件
		rdDistrictProjectMpMapper.selectByCondition(condition);
        PageData data=new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(rdDistrictProjectMpMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result add(RdDistrictProjectMp rdDistrictProjectMp, AccessToken accessToken) {
        rdDistrictProjectMp.setCreateBy(accessToken.getUserCode());
        rdDistrictProjectMpMapper.insert(rdDistrictProjectMp);
        return Response.response();
    }

    @Override
    public Result edit(RdDistrictProjectMp rdDistrictProjectMp, AccessToken accessToken) {
        rdDistrictProjectMpMapper.updateByPrimaryKey(rdDistrictProjectMp);
        return Response.response();
    }

    @Override
    public Result del(Integer id,AccessToken accessToken,String updateName) {
        int count = rdDistrictProjectMpMapper.deleteByPrimaryKey(id);
        return Response.response(count);
    }

    @Override
    public Result checkMp(String regionCode,AccessToken accessToken) {

        List<RdDistrictProjectMp> checkMp=rdDistrictProjectMpMapper.checkMp();
        List<String> NotIncommIdName=new ArrayList<>();
        for (int i=0;i<checkMp.size();i++){
            NotIncommIdName.add(checkMp.get(i).getProjectName());
        }
        List<DupComm> comms=managerMapForErpMapper.queryAllCommCaseDistrict(regionCode,null,null,null,NotIncommIdName);
        return Response.response(comms);
    }

}