package com.zd.laboratory.service.impl;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.domain.LabViolationScoreSite;
import com.zd.laboratory.mapper.LabViolationScoreSiteMapper;
import com.zd.laboratory.service.ILabViolationScoreSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabViolationSiteMapper;
import com.zd.laboratory.domain.LabViolationSite;
import com.zd.laboratory.service.ILabViolationSiteService;

/**
 * 违规-违���项设置Service业务层处理
 *
 * @author zd
 * @date 2021-12-13
 */
@Service
public class LabViolationSiteServiceImpl implements ILabViolationSiteService
{
    @Autowired
    private LabViolationSiteMapper labViolationSiteMapper;

    @Autowired
    private ILabViolationScoreSiteService labViolationScoreSiteService;

    @Autowired
    private LabViolationScoreSiteMapper labViolationScoreSiteMapper;

    /**
     * 查询违规-违���项设置
     *
     * @param id 违规-违���项设置主键
     * @return 违规-违���项设置
     */
    @Override
    public LabViolationSite selectLabViolationSiteById(Long id)
    {
        return labViolationSiteMapper.selectLabViolationSiteById(id);
    }

    /**
     * 查询违规-违���项设置列表
     *
     * @param labViolationSite 违规-违���项设置
     * @return 违规-违���项设置
     */
    @Override
    public List<LabViolationSite> selectLabViolationSiteList(LabViolationSite labViolationSite)
    {
        return labViolationSiteMapper.selectLabViolationSiteList(labViolationSite);
    }

    /**
     * 新增违规-违���项设置
     *
     * @param obj 违规-违���项设置
     * @return 结果
     */
    @Override
    public int insertLabViolationSite(Map obj)
    {
         Map map =  obj;
        int row = 0;
         if (map.get("violationType")!=null) {
//            List<Map<String, String>> siteList = (List<Map<String, String>>) map.get("violationType");
             labViolationSiteMapper.deleteLabViolationSiteAll();
             //todo 需求变动，取消了原来的violationType字段类型参数
//            siteList.forEach(s->{
//                LabViolationSite site = new LabViolationSite();
//                site.setViolationVal(s.get("violationVal"));
//                site.setRemake(obj.get("remake").toString());
//                labViolationSiteMapper.insertLabViolationSite(site);
//            });
             LabViolationSite site = new LabViolationSite();
             site.setViolationVal("负面清单次数");
             site.setRemake(obj.get("remake").toString());
             labViolationSiteMapper.insertLabViolationSite(site);
             row = 1;
         }
         if (map.get("violationMeth")!=null){
             List<Map<String, String>>  scoreSites = (List<Map<String, String>>) map.get("violationMeth");
             labViolationScoreSiteService.delScoreSiteAll();
             scoreSites.forEach(c->{
                 LabViolationScoreSite scoreSite = new LabViolationScoreSite();
                 scoreSite.setViolationVal(c.get("violationVal"));
                 scoreSite.setViolationName(c.get("violationName"));
                 labViolationScoreSiteService.insertLabViolationScoreSite(scoreSite);
             });
             row = 1;
         }
       return row;
    }

    /**
     * 修改违规-违���项设置
     *
     * @param labViolationSite 违规-违���项设置
     * @return 结果
     */
    @Override
    public int updateLabViolationSite(LabViolationSite labViolationSite)
    {
        return labViolationSiteMapper.updateLabViolationSite(labViolationSite);
    }

    /**
     * 批量删除违规-违���项设置
     *
     * @param ids 需要删除的违规-违���项设置主键
     * @return 结果
     */
    @Override
    public int deleteLabViolationSiteByIds(Long[] ids)
    {
        return labViolationSiteMapper.deleteLabViolationSiteByIds(ids);
    }

    /**
     * 删除违规-违���项设置信息
     *
     * @param id 违规-违���项设置主键
     * @return 结果
     */
    @Override
    public int deleteLabViolationSiteById(Long id)
    {
        return labViolationSiteMapper.deleteLabViolationSiteById(id);
    }

    @Override
    @DataScope(deptAlias = "", permi = PerPrefix.LABORATORY_SITE )
    public List<LabViolationSite> selectLabViolationSite() {
        return labViolationSiteMapper.selectLabViolationSite();
    }

    @Override
    public int removeIds(Map obj) {
        Map map =  obj;
        int row = 0;
        if (map.get("violationType")!=null) {
            List<Map<String, String>> siteList = (List<Map<String, String>>) map.get("violationType");
            siteList.forEach(s->{
                labViolationSiteMapper.deleteLabViolationSiteById(Long.valueOf(s.get("id")));
            });
            row = 1;
        }
        if (map.get("violationMeth")!=null){
            List<Map<String, String>>  scoreSites = (List<Map<String, String>>) map.get("violationMeth");
            scoreSites.forEach(c->{
                labViolationScoreSiteService.deleteLabViolationScoreSiteById(Long.valueOf(c.get("id")));
            });
            row = 1;
        }
        return row;
    }
}
