//package com.crunii.micro.service.sysmgr.service.impl;
//
//import cn.hutool.core.codec.Base64;
//import cn.hutool.core.util.ObjectUtil;
//import com.crunii.micro.common.dto.PageReq;
//import com.crunii.micro.common.dto.PageRsp;
//import com.crunii.micro.common.dto.Result;
//import com.crunii.micro.common.exception.BusinessException;
//import com.crunii.micro.common.utils.JsonUtil;
//import com.crunii.micro.domain.dto.Worker;
//import com.crunii.micro.domain.entity.main.YcqCockpitModel;
//import com.crunii.micro.domain.entity.main.YcqDynamicFormConfig;
//import com.crunii.micro.domain.repo.main.YcqCockpitModelRepo;
//import com.crunii.micro.domain.repo.main.YcqFormRepo;
//import com.crunii.micro.service.sysmgr.api.domain.dto.form.*;
//import com.crunii.micro.service.sysmgr.jdbc.YcqDynamicFormConfigDao;
//import com.crunii.micro.service.sysmgr.service.YcqDynamicFormConfigService;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.node.ObjectNode;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeanUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import javax.transaction.Transactional;
//import java.io.IOException;
//import java.util.*;
//
//@Service
//public class YcqDynamicFormConfigServiceImpl implements YcqDynamicFormConfigService {
//
//    private static final Logger log = LoggerFactory.getLogger(YcqDynamicFormConfigService.class);
//
//    private static final String FORM_CODE_PRE = "FORM";
//
//    @Resource
//    private YcqFormRepo formRepo;
//    @Resource
//    private YcqCockpitModelRepo modelRepo;
//    @Resource
//    private YcqDynamicFormConfigDao ycqDynamicFormConfigDao;
//
//    private final ObjectMapper objectMapper = JsonUtil.getMapper();
//
//    @Override
//    public YcqDynamicFormConfigVO insertForm(YcqDynamicFormConfigAddDTO ycqDynamicFormConfigAddDTO, Integer loginId) {
//        YcqDynamicFormConfig form = new YcqDynamicFormConfig();
//        BeanUtils.copyProperties(ycqDynamicFormConfigAddDTO, form);
//        form.setCode(FORM_CODE_PRE + new Date().getTime());
//        form.setCreateTime(new Date());
//        form.setCreateId(loginId);
//        form.setUpdateTime(new Date());
//        form.setUpdateId(loginId);
//        YcqDynamicFormConfig ycqDynamicFormConfig = this.formRepo.saveAndFlush(form);
//        YcqDynamicFormConfigVO ycqDynamicFormConfigVO = new YcqDynamicFormConfigVO();
//        ycqDynamicFormConfigVO.setId(ycqDynamicFormConfig.getId());
//        return ycqDynamicFormConfigVO;
//    }
//
//    @Override
//    public void updateBaseInfo(YcqDynamicFormConfigUpdateBaseDTO ycqDynamicFormConfigUpdateBaseDTO, Integer loginId) {
//        YcqDynamicFormConfig one = this.formRepo.getOne(ycqDynamicFormConfigUpdateBaseDTO.getId());
//        BeanUtils.copyProperties(ycqDynamicFormConfigUpdateBaseDTO, one, "id");
//        one.setUpdateId(loginId);
//        one.setUpdateTime(new Date());
//        this.formRepo.saveAndFlush(one);
//    }
//
//    @Override
//    public YcqDynamicFormConfigVO getById(Long formId) {
//        YcqDynamicFormConfig form = this.formRepo.getOne(formId);
//        YcqDynamicFormConfigVO one = new YcqDynamicFormConfigVO();
//        BeanUtils.copyProperties(form, one);
//        return one;
//    }
//
//    @Transactional
//    @Override
//    public Long updateFormHtml(YcqDynamicFormConfigUpdateDTO dto, Integer loginId) throws IOException {
//        YcqDynamicFormConfig one = this.formRepo.getOne(dto.getId());
//        if (null == one){
//            log.error("当前配置id：{}未查询到数据，请核查！", dto.getId());
//            throw new BusinessException("数据异常，请联系管理员");
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("name", dto.getFloModelName());
//        YcqCockpitModel model = this.modelRepo.getUniqueByProps(map);
//        if (null == model){
//            log.error("当前配置id：{}未查询到数据，请核查！", dto.getId());
//            throw new BusinessException("数据异常，请联系管理员");
//        }
//        Byte isTmp = one.getIsTmp();
//        if (Objects.equals(isTmp, (byte) 1)){
//            // 暂存数据调用该接口直接覆盖json
//            one.setFloModelId(model.getId());
//            one.setFloModelCode(model.getCode());
//            one.setHtml(dto.getHtml());
//            one.setUpdateTime(new Date());
//            one.setUpdateId(loginId);
//            one.setIsTmp((byte)0);
//            this.formRepo.saveAndFlush(one);
//            return dto.getId();
//        }
//        Long oldFormId = one.getId();
//        Long newFormId = oldFormId;
//        if (StringUtils.hasText(one.getHtml())) {
//            List<String> oldList = this.getFormLabelKey(Base64.decodeStr(one.getHtml()));
//            List<String> newList = this.getFormLabelKey(Base64.decodeStr(dto.getHtml()));
//            // 每次更新form表单时不去新增floForm的数据，只更新
//            if (new HashSet<>(oldList).containsAll(newList) && new HashSet<>(newList).containsAll(oldList)) {
//                // 如果历史字段全部包含在新字段中就更新不然就新增
//                one.setFloModelId(model.getId());
//                one.setFloModelCode(model.getCode());
//                one.setHtml(dto.getHtml());
//                one.setUpdateTime(new Date());
//                one.setUpdateId(loginId);
//                this.formRepo.saveAndFlush(one);
//            } else {
//                // 新增表单
//                one.setUpdateId(loginId);
//                one.setUpdateTime(new Date());
//                this.formRepo.save(one);
//                this.formRepo.flush();
//                YcqDynamicFormConfig form = new YcqDynamicFormConfig();
//                form.setCode(FORM_CODE_PRE + new Date().getTime());
//                form.setFloModelId(model.getId());
//                form.setFloModelCode(model.getCode());
//                form.setName(one.getName());
//                form.setDescription(one.getDescription());
//                form.setHtml(dto.getHtml());
//                form.setCreateTime(one.getCreateTime());
//                form.setCreateId(one.getCreateId());
//                form.setUpdateTime(new Date());
//                form.setUpdateId(loginId);
//                this.formRepo.saveAndFlush(form);
//                newFormId = form.getId();
//            }
//        } else {
//            one.setFloModelId(model.getId());
//            one.setHtml(dto.getHtml());
//            one.setUpdateTime(new Date());
//            one.setUpdateId(loginId);
//            this.formRepo.save(one);
//        }
//        return newFormId;
//    }
//
//    /**
//     * 逻辑删除
//     * @param
//     */
//    @Override
//    public void deleteById(Long id) {
//        YcqDynamicFormConfig one = formRepo.getOne(id);
//        if (ObjectUtil.isEmpty(one)||(ObjectUtil.isNotEmpty(one) && Objects.equals((byte)1, one.getDelFlag()))){
//            throw new BusinessException("表单不存在");
//        }
//        one.setDelFlag((byte)1);
//        formRepo.saveAndFlush(one);
//    }
//
//    @Override
//    public PageRsp<YcqDynamicFormConfigPageVO> pageForm(PageReq<YcqDynamicFormConfigPageDTO> req, Integer loginId) {
//        return ycqDynamicFormConfigDao.pageForm(req,loginId);
//    }
//
//    private List<String> getFormLabelKey(String html) throws IOException {
//        List<String> list = new ArrayList<>();
//        if (html == null || "".equals(html)) {
//            return list;
//        }
//        ObjectNode node = (ObjectNode) this.objectMapper.readTree(html);
//        JsonNode jsonNodeArr = node.get("schema");
//        for (JsonNode jsonNode : jsonNodeArr) {
//            String fieldKey = jsonNode.get("fieldKey").asText();
//            list.add(fieldKey);
//        }
//        return list;
//    }
//
//    @Override
//    public Result<YcqDynamicFormConfigVO> selectFormByCode(String formCode) {
//        Optional<YcqDynamicFormConfig> formOpt = this.formRepo.getLastForm(formCode);
//        if (formOpt.isPresent()) {
//            YcqDynamicFormConfigVO one = new YcqDynamicFormConfigVO();
//            BeanUtils.copyProperties(formOpt.get(), one);
//            return Result.ofSuccess(one);
//        }
//        return Result.ofFail("表单配置不存在");
//    }
//
//    @Override
//    @Transactional
//    public YcqDynamicFormConfigVO updateFormHtmlTmp(YcqDynamicFormConfigUpdateDTO dto, Integer loginId){
//        YcqDynamicFormConfig one = this.formRepo.getOne(dto.getId());
//        Map<String, Object> map = new HashMap<>();
//        map.put("name", dto.getFloModelName());
//        YcqCockpitModel model = this.modelRepo.getUniqueByProps(map);
//
//        one.setUpdateId(loginId);
//        one.setUpdateTime(new Date());
//        one.setHtml(dto.getHtml());
//        one.setFloModelId(model.getId());
//        one.setFloModelCode(model.getCode());
//        one.setIsTmp((byte) 1);
//        this.formRepo.save(one);
//        YcqDynamicFormConfigVO result = new YcqDynamicFormConfigVO();
//        BeanUtils.copyProperties(one, result);
//        return result;
//    }
//}
