package com.huatek.busi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dto.RParamBasicDataDto;
import com.huatek.busi.entity.RParamAttrBasicData;
import com.huatek.busi.entity.RParamBasicData;
import com.huatek.busi.mapper.RParamAttrBasicDataMapper;
import com.huatek.busi.mapper.RParamBasicDataMapper;
import com.huatek.busi.service.IRParamBasicDataService;
import com.huatek.busi.vo.DeviceSubclassOrTestItemListVo;
import com.huatek.busi.vo.RParamBasicDataVo;
import com.huatek.busi.where.RParamBasicDataSelectVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@Service(interfaceClass = IRParamBasicDataService.class)
@Transactional(rollbackFor = Exception.class)
public class RParamBasicDataServiceImpl extends BaseDao<RParamBasicData, String> implements IRParamBasicDataService {

    @Resource
    private RParamBasicDataMapper rParamBasicDataMapper;

    @Resource
    private RParamAttrBasicDataMapper rParamAttrBasicDataMapper;

    @Override
    public Map<String, Object> mainList(RParamBasicDataSelectVo rParamBasicDataSelectVo){
        PageInfo<RParamBasicDataDto> page = PageHelper.startPage(rParamBasicDataSelectVo.getPage(), rParamBasicDataSelectVo.getLimit())
                .doSelectPageInfo(() -> rParamBasicDataMapper.mainList(rParamBasicDataSelectVo));
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "BUSINESS.QUERY.SUCCESS", page.getList());
    }

    @Override
    public Map<String, Object> saveOrUpdate(String token, RParamBasicDataVo rParamBasicDataVo){
        RParamBasicData rParamBasicData = new RParamBasicData();

        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        // 获取当前用户信息
        LocalUser user = UserUtil.getLocalUser(token);

        String msg;
        int count = 0, code;
        String id = rParamBasicDataVo.getId();
        if(StrUtil.isNotBlank(id)){
            rParamBasicData = this.get(id);
            if(rParamBasicData != null){
                rParamBasicDataVo.setId(id);
                BeanUtils.copyProperties(rParamBasicDataVo, rParamBasicData);
                if(user != null){
                    rParamBasicData.setUpdateUser(user.getUid());
                    rParamBasicData.setUpdateTime(LocalDateTime.now());
                }
                count = this.updateLocal(rParamBasicData);
                if(count > 0){
                    msg = "BUSINESS.UPDATE.SUCCESS";
                    code = ConstantUtil.REQUEST_SUCCESS;
                }else {
                    msg = "BUSINESS.UPDATE.FAIL";
                    code = ConstantUtil.REQUEST_FAIL;
                }
            }else {
                msg = "BUSINESS.UPDATE.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }else {
            BeanUtils.copyProperties(rParamBasicDataVo, rParamBasicData);
            rParamBasicData.setId(Uuid.getUUID());
            if(user != null){
                rParamBasicData.setCreateUser(user.getUid());
                rParamBasicData.setCreateTime(LocalDateTime.now());
            }
            rParamBasicData.setDeleted(Constants.DEFAULT_NO);
            count = this.add(rParamBasicData);
            if(count > 0){
                msg = "BUSINESS.INSERT.SUCCESS";
                code = ConstantUtil.REQUEST_SUCCESS;
            }else {
                msg = "BUSINESS.INSERT.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }
        return ResultUtil.put(code, msg, count);
    }

    @Override
    public Map<String, Object> findOne(String token, String id){
        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        if(StrUtil.isEmpty(id)){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.ID.NOTNULL", "");
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", this.get(id));
    }

    @Override
    public Map<String, Object> delete(String token, List<String> ids){
        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        try {
            for(String id :ids){
                RParamBasicData rParamBasicData = this.get(id);
                if(rParamBasicData != null){
                    // 根据主表编号ID查询字表相关信息
                    String deleted = Constants.DEFAULT_NO;
                    List<RParamAttrBasicData> rParamAttrBasicDataList = rParamAttrBasicDataMapper.getRParamAttrBasicDataByPId(id, deleted);
                    if(CollectionUtil.isNotEmpty(rParamAttrBasicDataList)){
                        for (RParamAttrBasicData rParamAttrBasicData :rParamAttrBasicDataList){
                            deleted = Constants.DEFAULT_YES;
                            String pId = rParamAttrBasicData.getPId();
                            rParamAttrBasicDataMapper.updateByPId(pId, deleted);
                        }
                    }

                    rParamBasicData.setDeleted(Constants.DEFAULT_YES);
                    this.updateLocal(rParamBasicData);
                }
            }
        }catch (Exception e){
            log.error("删除异常", e);
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.DELETE.FAIL", "");
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.DELETE.SUCCESS", "");
    }

    @Override
    public RParamBasicDataDto findByParamValue(String paramKey){
        return rParamBasicDataMapper.findByParamValue(paramKey);
    }

    @Override
    public int insert(RParamBasicDataDto rParamBasicDataDto){
        RParamBasicData rParamBasicData = new RParamBasicData();
        BeanUtils.copyProperties(rParamBasicDataDto, rParamBasicData);
        return this.add(rParamBasicData);
    }

    @Override
    public RParamBasicDataDto findBySystemCodeAndTestItem(String systemCode, String testItem){
        return rParamBasicDataMapper.findBySystemCodeAndTestItem(systemCode, testItem);
    }

    @Override
    public List<DeviceSubclassOrTestItemListVo> testItemList(String equType) {
        return rParamBasicDataMapper.testItemList(equType);
    }
}
