package com.unis.crk.violation.service.impl;

import cn.hutool.json.JSONObject;
import com.unis.crk.violation.entity.ViolationWoring;
import com.unis.crk.violation.mapper.ViolationWoringMapper;
import com.unis.crk.violation.service.IViolationWoringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.unis.crk.violation.entity.vo.ViolationWoringVo;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 表E.34 违规预警信息 服务实现类
 * </p>
 *
 * @author Wys
 * @since 2023-06-15
 */
@Service
public  class ViolationWoringServiceImpl extends ServiceImpl<ViolationWoringMapper, ViolationWoring> implements IViolationWoringService {

    @Resource
    private ViolationWoringMapper violationWoringMapper;

    private String generateID(String unitid){
        String curDate=new SimpleDateFormat("yyyyMMdd").format(new Date());
        String ret="";
        String sql="select concat(ifnull(u.org_code,''),'"+curDate+"',right(cast(10000+cast(ifnull(max(replace(yjxxdm,CONCAT(ifnull(u.org_code,''),'"+curDate+"'),'')),'0')+1 as signed) as char),3)) as \"maxcode\" \n" +
                "from org_info u \n" +
                "LEFT JOIN Violation_Woring w on w.unitid=u.org_id\n" +
                "WHERE u.org_id='"+unitid+"'\n" +
                "GROUP BY u.org_code";
        List<Map<String,Object>> lt = violationWoringMapper.superSelect(sql);
        if (lt.size()>0){
            ret=lt.get(0).get("maxcode").toString();
        }
        return ret;
    }
    private List<Map<String,Object>> getUnitInfo(String unitid){
        return violationWoringMapper.superSelect("select org_name as \"orgname\",ifnull(credit_code,'无') as \"creditcode\" from org_info where org_id='"+unitid+"'");
    }
    /**
     *保存表E.34 违规预警信息
     * @param unitid 单位ID
     * @param entity 表E.34 违规预警信息业务数据
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveData(String unitid,ViolationWoringVo entity){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        List<Map<String,Object>> ltUnit=getUnitInfo(unitid);
        if((ltUnit==null)||(ltUnit.size()==0)){
            jsonObject.set("msg","保存预警信息失败！获取单位信息失败！");
            return  jsonObject;
        }
        ViolationWoring item=new ViolationWoring();
        //预警信息代码[库区代码+日期(8 位年月日)+顺序码(3 位)
        String maxCode=generateID(unitid);
        if (StringUtils.isBlank(maxCode)){
            jsonObject.set("msg","保存预警信息失败！生成预警信息代码失败！");
            return  jsonObject;
        }
        item.setYjxxdm(maxCode);
        item.setUnitid(unitid);
        item.setSjkd(ltUnit.get(0).get("orgname").toString());
        item.setQydm(ltUnit.get(0).get("creditcode").toString());
        item.setCph(entity.getCph());
        item.setFbsj(entity.getFbsj());
        item.setWgxwr(entity.getWgxwr());
        item.setWgztlx(entity.getWglx());
        item.setWgqk(entity.getWgqk());
        item.setCzzt("01");
        item.setCzbz("i");
        item.setZhgxsj(new Date());

        boolean b = super.saveOrUpdate(item);
        if (!b) {
            jsonObject.set("msg","保存预警信息失败");
            return  jsonObject;
        }
        jsonObject.set("success",true);
        jsonObject.set("msg","保存成功");
        return jsonObject;
    }

    /**
     * 修改表E.34 违规预警信息
     *
     * @param entity 表E.34 违规预警信息业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(ViolationWoring entity) {
        return null;
    }

    /**
     *删除表E.34 违规预警信息数据
     * @param id 表E.34 违规预警信息主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id){
        return null;
        }

    /**
     * 按照条件查询表E.34 违规预警信息信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(ViolationWoringVo pageQuery){
        //条形查询器
            /*
        QueryWrapper<ViolationWoring> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getBizNo())){
        queryWrapper.like("bizNo",pageQuery.getBizNo());
        }
        //分页相关
        Page<ViolationWoring> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

        IPage<Map<String,Object>> page = violationWoringMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
        */
            return null;
    }
}
