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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.dup.CaseFieldInfoMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.CaseFieldInfo;
import cn.com.countrygarden.bi.main.service.dup.CaseFieldInfoService;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by huangzhengjun on 2018/2/8.
 */
@Service
@Transactional
public class CaseFieldInfoServiceImpl implements CaseFieldInfoService {

    @Autowired
    private CaseFieldInfoMapper caseFieldInfoMapper;

    @Override
    public int insertCaseFieldInfo(Map<String, Object> paramMap) {

        CaseFieldInfo caseFieldInfo = new CaseFieldInfo();


        caseFieldInfo.setOrganName(paramMap.get("organName") == null ? null : String.valueOf(paramMap.get("organName")));
        caseFieldInfo.setOrganCode(paramMap.get("organCode") == null ? null : String.valueOf(paramMap.get("organCode")));
        caseFieldInfo.setCommId(paramMap.get("commId") == null ? null : String.valueOf(paramMap.get("commId")));
        caseFieldInfo.setCommName(paramMap.get("commName") == null ? null : String.valueOf(paramMap.get("commName")));
        caseFieldInfo.setCreatedBy(paramMap.get("createdBy") == null ? null : String.valueOf(paramMap.get("createdBy")));
        caseFieldInfo.setCaseFieldName(paramMap.get("caseFieldName") == null ? null : String.valueOf(paramMap.get("caseFieldName")));
        caseFieldInfo.setRemark(paramMap.get("remark") == null ? null : String.valueOf(paramMap.get("remark")));

        int i = caseFieldInfoMapper.insertCaseFieldInfo(caseFieldInfo);

        return i;
    }

    @Override
    public Map<String, Object> modifyCaseFieldInfo(Map<String, Object> paramMap) {

        CaseFieldInfo caseFieldInfo = new CaseFieldInfo();
        int id = Integer.parseInt(String.valueOf(paramMap.get("id")));
        caseFieldInfo.setId(paramMap.get("id") == null ? null : Integer.parseInt(String.valueOf(paramMap.get("id"))));
        caseFieldInfo.setOrganName(paramMap.get("organName") == null ? null : String.valueOf(paramMap.get("organName")));
        caseFieldInfo.setOrganCode(paramMap.get("organCode") == null ? null : String.valueOf(paramMap.get("organCode")));
        caseFieldInfo.setCommId(paramMap.get("commId") == null ? null : String.valueOf(paramMap.get("commId")));
        caseFieldInfo.setCommName(paramMap.get("commName") == null ? null : String.valueOf(paramMap.get("commName")));
        caseFieldInfo.setUpdatedBy(paramMap.get("updatedBy") == null ? null : String.valueOf(paramMap.get("updatedBy")));
        caseFieldInfo.setCaseFieldName(paramMap.get("caseFieldName") == null ? null : String.valueOf(paramMap.get("caseFieldName")));
        caseFieldInfo.setRemark(paramMap.get("remark") == null ? null : String.valueOf(paramMap.get("remark")));

        int i = caseFieldInfoMapper.modifyCaseFieldInfo(caseFieldInfo);
        Map<String, Object> stringObjectMap = this.queryById(id);
        if (i > 0) {
            stringObjectMap.put("result","修改成功");

        }else{
            stringObjectMap.put("result","修改失败");
        }

        return stringObjectMap;

    }

    @Override
    public int deleteCaseFieldInfoById(int id, String userCode) {
        int i = caseFieldInfoMapper.deleteCaseFieldInfoById(id, userCode);

        return i;
    }

    @Override
    public Result queryCaseFieldInfo(Map<String, Object> paramMap) {
        List<Map<String, Object>> resultList = new ArrayList<>();

        String organCode = String.valueOf(paramMap.get("organCode"));
        String commId = String.valueOf(paramMap.get("commId"));
        List<String> commIds = paramMap.get("commIds") == null ? null : (List<String>) paramMap.get("commIds");
        List<String> organs = paramMap.get("organs") == null ? null : (List<String>) paramMap.get("organs");
        String caseFieldId = String.valueOf(paramMap.get("caseFieldId"));
        int page = Integer.parseInt(String.valueOf(paramMap.get("page")));
        int size = Integer.parseInt(String.valueOf(paramMap.get("size")));

        List<CaseFieldInfo> caseFieldInfos = caseFieldInfoMapper.queryCaseFieldInfo(organCode,organs, commId, commIds, caseFieldId, (page - 1) * size, size);

        int total = caseFieldInfoMapper.queryTotal(organCode, commId,commIds, caseFieldId);
        PageRowBounds pageRowBounds = new PageRowBounds(page, size);
        pageRowBounds.setCount(true);
        pageRowBounds.setTotal((long) total);
        if (caseFieldInfos != null && caseFieldInfos.size() > 0) {
            for (CaseFieldInfo caseFieldInfo : caseFieldInfos) {
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put("id", caseFieldInfo.getId() == null ? "" : caseFieldInfo.getId());
                tempMap.put("organName", caseFieldInfo.getOrganName() == null ? "" : caseFieldInfo.getOrganName());
                tempMap.put("organCode", caseFieldInfo.getOrganCode() == null ? "" : caseFieldInfo.getOrganCode());
                tempMap.put("commId", caseFieldInfo.getCommId() == null ? "" : caseFieldInfo.getCommId());
                tempMap.put("commName", caseFieldInfo.getCommName() == null ? "" : caseFieldInfo.getCommName());
                tempMap.put("caseFieldName", caseFieldInfo.getCaseFieldName() == null ? "" : caseFieldInfo.getCaseFieldName());
                tempMap.put("remark", caseFieldInfo.getRemark() == null ? "" : caseFieldInfo.getRemark());
                resultList.add(tempMap);
            }
        }
        PageData pageData = new PageData(resultList, pageRowBounds);
        return Response.response(pageData);
    }

    @Override
    public Map<String, Object> queryById(int id) {

        CaseFieldInfo caseFieldInfo = caseFieldInfoMapper.queryById(id);
        Map<String, Object> tempMap = new HashMap<>();
        tempMap.put("id", caseFieldInfo.getId() == null ? "" : caseFieldInfo.getId());
        tempMap.put("organName", caseFieldInfo.getOrganName() == null ? "" : caseFieldInfo.getOrganName());
        tempMap.put("organCode", caseFieldInfo.getOrganCode() == null ? "" : caseFieldInfo.getOrganCode());
        tempMap.put("commId", caseFieldInfo.getCommId() == null ? "" : caseFieldInfo.getCommId());
        tempMap.put("commName", caseFieldInfo.getCommName() == null ? "" : caseFieldInfo.getCommName());
        tempMap.put("caseFieldName", caseFieldInfo.getCaseFieldName() == null ? "" : caseFieldInfo.getCaseFieldName());
        tempMap.put("remark", caseFieldInfo.getRemark() == null ? "" : caseFieldInfo.getRemark());
        return tempMap;

    }
}
