package com.huatek.busi.service.impl;

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.ServerFilePathDto;
import com.huatek.busi.entity.ServerFilePath;
import com.huatek.busi.mapper.ServerFilePathMapper;
import com.huatek.busi.service.IServerFilePathService;
import com.huatek.busi.vo.ServerFilePathVo;
import com.huatek.busi.where.ServerFilePathSelectVo;
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 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;

@Component
@Service(interfaceClass = IServerFilePathService.class)
@Transactional(rollbackFor = Exception.class)
public class ServerFilePathServiceImpl extends BaseDao<ServerFilePath, String> implements IServerFilePathService {

    @Resource
    private ServerFilePathMapper serverFilePathMapper;

    @Override
    public PageInfo<ServerFilePathDto> list(ServerFilePathSelectVo serverFilePathSelectVo) {
        return PageHelper.startPage(serverFilePathSelectVo.getPage(), serverFilePathSelectVo.getLimit())
                .doSelectPageInfo(() -> serverFilePathMapper.list(serverFilePathSelectVo));
    }

    @Override
    public Map<String, Object> saveOrUpdate(ServerFilePathVo serverFilePathVo, String token){

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

        LocalUser user = UserUtil.getLocalUser(token);

        String msg;
        int count = 0, code;
        ServerFilePath serverFilePath = new ServerFilePath();
        String id = serverFilePathVo.getId();
        if(StrUtil.isEmpty(id)){
            serverFilePathVo.setId(Uuid.getUUID());
            if(user != null){
                serverFilePathVo.setCreateUser(user.getUid());
                serverFilePathVo.setCreateTime(LocalDateTime.now());
            }
            serverFilePathVo.setDeleted(Constants.DEFAULT_NO);
            BeanUtils.copyProperties(serverFilePathVo, serverFilePath);
            count = this.add(serverFilePath);
            if(count > 0){
                msg = "BUSINESS.INSERT.SUCCESS";
                code = ConstantUtil.REQUEST_SUCCESS;
            }else {
                msg = "BUSINESS.INSERT.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }else {
            if(this.get(id) != null){
                serverFilePathVo.setId(id);
                if(user != null){
                    serverFilePathVo.setUpdateUser(user.getUid());
                    serverFilePathVo.setUpdateTime(LocalDateTime.now());
                    serverFilePathVo.setDeleted(Constants.DEFAULT_NO);
                }
                BeanUtils.copyProperties(serverFilePathVo, serverFilePath);
                count = serverFilePathMapper.update(serverFilePath);
                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;
            }
        }
        return ResultUtil.put(code, msg, count);
    }

    @Override
    public Map<String, Object> findOne(String id){
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", this.get(id));
    }

    @Override
    public Map<String, Object> delete(List<String> ids){
        serverFilePathMapper.batchDel(ids);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.DELETE.SUCCESS", "");
    }

    @Override
    public ServerFilePath findBySiteIdAndSystemCode(String siteId, String systemCode){
        return serverFilePathMapper.findBySiteIdAndSystemCode(siteId, systemCode);
    }
}
